In how many ways can we get data from one page to another page in PHP

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 database and needing to retain the same for the specific user across the website. In PHP, there are many different ways for 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 as 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 at 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 show the user-id. Since this system is secure, a member cannot change the value.

Example

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 session, remember to start session using 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 is less sure method to pass value. So never send sensitive value like password using this method unless you want to persist data for longer period.

Example

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 password etc.

Example:

http://discussdesk.com/index.php?id=5&name=discussdesk

We can the value id using GET method

$id = $_GET[‘id’];

$name = $_GET[‘name’];

Note:

1.  $_GET method is not secure. Never send any sensitive data using URL method.

2. We cannot send more amounts of data by this method.

Passing Value using POST method

By using POST method in form 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.

Example

Here in form we just set form action to post <form action=”POST”>.And we can get any data value inside this form using

$id=$_POST['id'];

Use of Web Forms to pass values between pages

Commonly web forms are used to gather data and then transfer the same to processing page. The processing page makes use of any of the above technique to pass variables to different pages of the website. Web Forms 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, following are mentioned some standard methods and reasons of use for the same.

The below briefly explain the use of each method separately.

Uses

Method

Reasons

Login Form containing User Id and User Password

POST

The User Id and User Password is invisible in 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 user’s device

Log In Status, whether a user is logged in

Session

Secure Data

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments