More often we need to pass values between pages in PHP. This functionality can be enabled for a website in many different ways. At times we require getting value from the database and needing to retain the same for the specific user across the website. In PHP, there are many different ways of passing such variables. This can be done within a site or outside of it. Here are many ways we can get data from one page to another page in PHP.
Passing values between pages by using session
This is considered a secured method to pass values between pages. One of the good examples of this system is when we spot used-id inside of a member zone once we logged in. In a member login system, the details of the user are verified and if found true a new session is generated. This value is stored on the server-side together with the used-id of the member. Next, when a new page is opened in an Internet browser, the server makes a check for the session value and shows the user-id. Since this system is secure, a member cannot change the value.
In One Page, We can just set the value in session
$_SESSION['session_name'] = $session_value;
And we can get this in any of the page till the browser close or session expire.
$get_session_value = $_SESSION['session_name'];
Note: before using the session, remember to start a session using the session_start() statement on top of the page or before any output sent to the browser.
Passing values between pages using cookies
We know that cookies are stored at the client end or user end and variables can be passed between pages in PHP. However, the client’s internet browser can change the security settings and thus reject accepting cookies. Thereby the system may fail in passing variables between pages. In such conditions cookies are found useful in handling user-entered variables and then passing those between pages.
Note: cookies are a less sure method to pass value. So never send sensitive value like password using this method unless you want to persist data for a longer period.
In One Page, we can set value in cookie.
setcookie("set_cookie_name", $cookie_value, time()+3600); /* expire in 1 hour */
In other page, we can get the same value using cookie get method.
$var_value = $_COOKIE['set_cookie_name'];
Passing values between pages using URL
In PHP, variables can be passed through pages by using URL i.e. in the address bar of a web browser. In this scenario, users can view the values in the address bar as well as in the browser’s history folder. This method is not considered secure for transferring sensitive data for example users’ profile details or passwords etc.
We can the value id using GET method
$id = $_GET[‘id’];
$name = $_GET[‘name’];
1. $_GET method is not secure. Never send any sensitive data using the URL method.
2. We cannot send more amounts of data by this method.
Passing Value using the POST method
By using the POST method in the form of submission, we can send more amounts of data from one page to another page. We can send more secure data like password, online transaction, etc using this method as data is not visible.
Here in the form we just set form action to post
And we can get any data value inside this form using
Commonly web forms are used to gather data and then transfer the same to the processing page. The processing page makes use of any of the above techniques to pass variables to different pages of the website. WebForms are also used to pass variables between different websites. Also, Web Forms can submit the values to a processing script working at a different URL in some other server.
There is no solid rule to use one particular type of method to pass values between pages in PHP. Any data coming from a distinct source requires refining before being taken in use. However, the following are mentioned some standard methods and reasons for use for the same.
The below briefly explain the use of each method separately.
|Login Form containing User Id and User Password||POST||The User Id and User Password is invisible in the query string|
|Sending Product Id||GET||User can save the URL in a Bookmark for future reference|
|Feedback Form||POST||Data can be posted multiple times.|
|User Pattern for example browsing habits||Cookies||Data is stored in the user’s device|
|Log In Status, whether a user is logged in||Session||Secure Data|