Pass Variables from One Page to Another -- PHP
Author: D.Shaun Morgan
- PHP Version - PHP 5x
- Reader skill level - Beginner
- XHTML 1.0 Transitional
Tutorial Outline:
|
|
Introduction
In this part of the PHP forms tutorial I will touch on four subjects. How to access PHP variables from one page to another using 1.) POST, 2.) GET, 3.) $_SESSION, 4.) arrays. You will learn about sending variables to a script and what to do with them.
Pass post data to another page
Here is a simple example of how to pass a PHP variable from one page to another with the post method, and how to use the POST super global array to make your coding experiences more efficient and less time-consuming. Let's start with another HTML form. In this form I need to collect 5 first names at one time.
Form example No. 1
<form method="post" action="array_script.php">
0.<br/>
<input type="text" name="first_name0"/><br/><br/>
1.<br/>
<input type="text" name="first_name1"/><br/><br/>
2.<br/>
<input type="text" name="first_name2"/><br/><br/>
3.<br/>
<input type="text" name="first_name3"/><br/><br/>
4.<br/>
<input type="text" name="first_name4"/><br/><br/>
<input type="submit" name="submit"/>
</form>
Values sent to the server corresponding to "first_name0" through " first_name4" will each occupy its' own space in the $_POST super global array. Now, let's write a piece of code on how to get those variables from the server to my script "array_script.php."
<?php
$first_name0 = $_POST['first_name0'];
$first_name1 = $_POST['first_name1'];
$first_name2 = $_POST['first_name2'];
$first_name3 = $_POST['first_name3'];
$first_name4 = $_POST['first_name4'];
?>
Now, I can use the variables that I sent over from my form. That is the simplest explanation of how to pass variables from one page to another. I also could have used "get" instead of "post" for the "method" attribute in (Form example No. 1). In that case, I would replace "$_POST" with "$_GET" in the script. One difference is that if you use $_GET to access a variable, users can inject any value they want, and send it to your script. In some cases this may present a serious security problem. "get," however, makes it possible for you to pass variables via links.
Click here for an
example on passing whole arrays with a form.
--------------------------------------------------------------------------------------------------------------------
Example passing variables using Method $_GET
<a href="http://yourdomain.com/test1.php?first_name=Shaun&last_name=Morgan">Test Page 1</a>
Place the code below at the top of your target page.
<?php
$first_name = $_GET['first_name'];
$last_name = $_GET['last_name'];
?>
If my intentions are to pass on the values of "$first_name and $last_name" to yet another page I could use method -get ,or structure a link like the following.
<a href="http://yourdomain.com/test2.php?first_name=<?php echo $first_name?>&last_name=<?php echo $last_name?>">Test Page 2</a>
I can also pass variables in the "action" attribute of the form.
This method of passing values in php only works if the user clicks on the links that I have structured. This proves handy when I only want the user to choose a certain link, or when I want the variable I passed to only be available to the script that the link targets. I've found this very useful for writing scripts that handle data from a database. The $_GET method should not be used for security sensitive stuff unless other mesures to encrypt and secure the data have been taken. When a person clicks on the link or submits a form using this method, all of that data is veiwable, and can be edited from the browser's navigation bar, opening the door for hackers to put together scripts and programs to send malicious queries to your server at-will.
Please note that I have created local variables and set them to the value of the $_GET variables. I don't HAVE to do this to use the $_GET variables in my script . I could have used each of those $_GET variables by themselves. Personally I think it makes for a much neater code to copy those variables' values into new local variables. By doing this consistently, I find that it helps me to keep track of the variables I am using.
--------------------------------------------------------------------------------------------------------------------
Example passing variables using $_SESSION
If instead of coding my variables in links, I would rather store them on my server somewhere so that I can come back to them later and pass variables one file to another, one very easy way to accomplish this is using $_SESSION. When you put a value into the $_SESSION super global, it will be available on your server until the session times-out.
If you don't already have it, now is a good time to surf over to php.net and get a copy of the php manual. If you are running windows, it comes in an easy-to-use help program. You can download it here http://www.php.net/download-docs.php. You will find this tool invaluable in your php coding efforts.
A session, simply, is when a person visits my site and I have called the "session_start()" php function. That session will last either until the session expires (this value is set in the php.ini file), or until the user closes his or her browser. Any data that I collect and place in $_SESSION will be available to all the scripts on my server while the session is still good. My server will assign each session an unique id to identify it. Any information I gather from session will then be saved in a text-file somewhere on my machine, depending on the php.ini settings.
Form example $_SESSION
First send the variable to a script
<form method="post" action="array_script.php">
0.<br/>
<input type="text" name="first_name"/><br/><br/>
<input type="submit" name="submit"/>
</form>
Move $_POST variable contents into $_SESSION
I will always need to call "session_start()" to use $_SESSION
<?php
session_start();
$_SESSION['first_name'] = $_POST['first_name'];
?>
Above I called the the session_start() function, created a session variable, and set that variable the values sent over via POST. Now I can access that $_SESSION variable from any page on my server.
Accessing $_SESSION Variable
I will always need to call "session_start()" to use $_SESSION
<?php
session_start();
$first_name = $_SESSION['first_name']
echo $first_name;
?>