Category: php


Pass Variables from One Page to Another -- PHP

Author: D.Shaun Morgan

Tutorial Outline :

Introduction - php Passing Variables between Pages

In this tutorial about passing php variables, you will learn how to access PHP variables from one page to another .

You will cover the following subjects:

  • POST
  • GET
  • $_COOKIE
  • Arrays
  • passing variables between scripts

Get The PHP Manual

The php manual is an indispensable tool for a php developer. It can be found at . If you are running windows, it comes in an easy-to-use help program. You can download it here

Passing 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. This tutorial will illustrate how to collect information with a form, in links, and by reading cookies.

The best way to learn any programming language is by doing it yourself. So, we will start this lesson by creating php files:

  1. First create a file named passing-vars.php .
  2. Second create an HTML FORM on the passing-vars.php file to collect user data.
  3. Third you create a file named catching-vars.php and then write some php code in it to handle the variables.

Explanations and How To

If you are new to this stuff, the explanations in the following list should help explain things a little better.

  • How to create php files --To create a php file, simply open a text editor like notepad and save your file as YourFile.php
  • What is Hosting ?-- A hosting account is a server that your website resides on. You will need a Web server like Apache installed on your PC with php or a hosting account somewhere with php to follow these examples.
    You may want to look at Installing Apache and Installing PHP for instructions on setting up apache and php on your local machine.
  • FORM -- Forms are used to collect text data from internet users. It is made up of text boxes and submit-buttons. Data collected with FORMS can vary greatly depending on what type of website a person is operating. for example has a FORM at the bottom of each page for collecting user feedback and comments about the website. When the Submit button is clicked, whatever text the user typed into the FORM'S input areas will be passed along to the web server which uses the value in the input box's -- name=variablename attribute to create global variables that live in the server as super-global-arrays – POST, or GET. For now it is enough to know that those variables have a global scope, and are usually sent to the script that you have pointed your form to using the form tag's action="". The example form below is pointing to a script called catching-var.php. catching-var.php will be responsible for processing the data passed to it from the form you place on passing-var.php.

Creating a Form for passing-var.php

Copy the HTML below, paste it into a text editor and then save the file as "passing-var.php" with a php file extension

This FORM is using the post method. You will notice when you submit it that there will be nothing extra in the address bar unlike some other forms you may have used. This is because the POST method does not pass information in the address bar.

Data values corresponding to "name0" through "name4" will each occupy their own space in the $_POST super global array on your server. Now, we will write some php code that makes it possible to pass variable between pages. This code will show you how to access the data that is sent to the POST global variable via an HTML form.

Copy the PHP code below and paste it into a text editor and then save the file as "catching-var.php."

Notice the syntax above. $_POST is like a container that holds information sent to it via the POST method.

Visual example of Super Globals

Testing the Post Script

Open your web browser and navigate to passing-var.php. Insert some data into the form and submit it.


Now the catching-var.php script has access to the data sent over to it by the HTML FORM your created -- passing-vara.php . If all is working correctly, you should see the data that you entered in output on catching-var.php.


Post Summary

That concludes the POST tutorial on learning how to pass variables from one page to another in php. Next we will go over using the "GET"method in your form's "action attribute" to pass php values in between web pages.

Passing variables in URI with Form Method $_GET

Passing variables in the hyperlink is versatile and useful to web programmers. And the "GET METHOD" with HTML Forms is especially useful because it allows you to see those variables in your web browser's address bar as they are sent by your php script as along with the values stored in them as they are passed to the another page of php script.

Let's Take a look at the following example.

GET Anchor / Hyperlink Link Example

If you read the POST tutorial then you have already learned how to make php files with a text editor. If not, then need to go and get a good text editor. I suggest notepad++. It is chocked full of features for just about every kind of programming language you can think of; and the best thing about it is -- it is free. It will cost you nothing; not a dime! However, for those of you who can afford it, I strongly encourage you to make donations to the author of Notepad++ for giving us all such a great text editor. AlSO JUST FYI. I get nothing for promoting it. I plugged it because I honestly like it.

Now that you have a great text editor...

It is time to learn how to write php for the GET METHOD Used with HTML forms.

Complete the Following Steps:

  1. Create two .php files -- passing-var.php and catching-var.php.
  2. Place those files into a directory on your web server that is able to serve php scripts to the public.
  3. On Apache it is usually called htdocs
  4. With Windows IIS, I think it is usually called www(But don't quote me on that -- I am an Apache man).
  5. The goal either way is the same -- to place your php scripts into a directory that can serve them.

Now that you have created your test php files we can start learning php in the context of the GET/HTML/LINK/FORM Method. To start this lesson we will hack up -- HACK!!! HACK!!! HACK!!!, Excuse me, got a lil choked there!! -- a line of text that will utilize the infamous(?) HTML anchor tag, sometimes referred to as an A Pronounced: A-EE tag, or for those of us who really like to buck the system and STICK IT TO THE MAN WE LIKE TO CALL IT ...SUSPENSE - DRUMROLL...



All kidding aside, one of the things I want to make clear about using the GET method of passing variables from one page to another page in PHP versus using the POST method, is how much more versatile GET can be when dealing with user input that changes often,dynamic variable data, and sending that data to the next page to be processed by a php script.

Coding variables into hyperlinks is easy. You actually see those variables all time while you are browsing the Internet. All the cryptic text that you see after a Question Mark "?" in URL's, you know, in the address bar / navigation bar, is a string that represents variable names and data being sent to the next page. They are all paired up then passed to another php page for processing. Take another look at the hyperlink from earlier.

Notice ?name=Shaun&Lname=Morgan.

When you follow that link to "catching-var.php" :

These vars ----- > name=Shaun&Lname=Morgan

will be available to catching-var.php because there is two new variables named name and Lname in the $_GET super global array with data, Shaun&Morgan stored in them respectively.

Now place the following code at the top of the php script that we created called catching-var.php

Notice the values of the name and Lname variables were passed into the "value=" attribute of the form tag.

Example How to Use GET Method - HTML FORMS

After submitting this information to the catching-var.php file, lets do something a little different. Instead of just showing you how to access the GET variable lets do something useful with that data. The next example will show you how you can code the data that was sent into yet another URL to be passed on for more processing. Create a link on your catch-var.php page like the one in the next example.

The resulting link would be:

You can also pass variables in the "action" attribute of the form tag like this:

This method of passing values in php only works if the user clicks on the links that were structured to pass values in the URL using the get method or typing information into a text box within a form tag that is using the "get" method.

Security Concerning the GET Method

The "get method" passes data in the URI. If you look at your web browser's address bar after submitting a "get method" form, you will notice that you can view all variables and values pairs that were written into the page's html forms and links. They can also be modified and resubmitted by the user which makes GET data insecure by itself.

Malicious parameters can be added to GET values in the web browser in an attempt to launch an injection attack against a web server. People who perpetrate injection attacks attempt to cause harm to remote machines by sending servers parameters often encoded into a web browser's address bar. PHP programmers should always watch out for this kind of of attack and be proactive about stopping attackers before they compromise their networks. The following steps can help keep attacks of this nature under control.

  1. It is also wise to use a function such as striptags() to keep attackers from injecting unwanted HTML and other code into your web site. The striptags() php function removes html and other scripting tags from the content it processes.
  2. Consider writing code to keep track of how many times a web attacker attempts to fill in the same form over and over. If a user is trying to fill a form out continuously, it is probably a spam bot trying to crack your system --- Or a really bored teenager doing what teenagers do -- tearing up stuff!
  3. Change passwords often.

The $_GET method in it's is not secure. The POST Method is slightly more secure than the GET method because a user cannot stand behind you and see the variable values you are sending. In today's cyber-landscape of 15 year old semi-skilled-hackers that invest hundreds of hours into getting fat on donuts, and learning how to break into other people's computers and phones, one way to protect your web site data is with encryption. Encryption takes all the traffic entering and leaving a network and turns it into human unreadable streams of bits that require credentials of some type to be viewed by the end user.

Example Passing Variables Using $_SESSION['']

What if I want to pass a certain set of variables over to every page on my site? Do I have to keep coding $_POST or $_GET into the HTML on every page?

Answer: No - You can use Sessions Or Cookies Instead.

SESSIONS will allow you to pass php variables and values to every page a user visits on your web site.


  • Every time a person visits a site that uses php session_start() a new session is started.
  • The user is also assigned an unique SESSION id number
  • Sessions are valid until the session expires (the expiry value is set in the php.ini file), or until the user closes his or her browser.
  • SESSONS are php super global arrays
  • The syntax for accessing session variables is -- $_SESSION['name of variables'].
  • Using Sessions makes passing variables between pages much easier.
  • $_SESSION['name of variables']is available to programmers anywhere on the server that he/she invokes the session_start(); php function.

Example of Form Tag and $_SESSION[''] Variables

First send the variable to a script

Move $_POST Variable Contents into $_SESSION Variable

Call: "session_start()": on testScript.php

Explanation of Example Script

First we use a form to send POST data to a php script -- testScript.php

Next testScript.php calls up the session_start() function, creating a session variable --- $_SESSION['name'] ---and sets it to the value found in $_POST['name']values. Now the $_SESSION['name'] variable can be accessed and used from any page on the server

Accessing $_SESSION Variable

You must call the "session_start()" function in php to gain access to the $_SESSION super global array.

Example on How to Use SESSIONS

Example About Using Cookie Variables

Using Cookie Variables php
Author: D.Shaun Morgan

stop form spam captcha php PHP -- How to Protect My Email Form / Contact Form Against Spam Bots Author: D.Shaun Morgan Versions and Skill Level PHP 5

how to make money online php Making Money on the Internet Sign up for this series of free money making articles Name:

php example post array php Visual example of how PHP handles the super-global array variable "$_POST" Author: D.Shaun Morgan Versions and Skill Level

php arrays php Passing arrays to another page Author: D.Shaun Morgan Versions and Skill Level PHP Version - PHP 5x Reader skill level

php passing variables php Pass Variables from One Page to Another -- PHP Author: D.Shaun Morgan Tutorial Outline : Passing PHP variables with $_


Leave a Comment


Name:      | Email:      | Website:



Enter The Above Security Code:

Author: sachin | Website URL: |
I am working on a small web site.I use form action method for login but in same page i give a searching criteria to show specific data. but when click on search button it show the path of login but I want to show search page how can I do this?
It sounds like you are probably using the same form tag for both actions. Create two forms on your page -- one for searches and the other for logging in. Place the search text boxes and button in the search form and put the login textbox and button in the login form.

Also it may be helpful and keep things simpler and easier to manage if you create locate the action scripts in two separate files instead of trying to put them on the same page. Doing this makes our code much more manageable.

Another great way to implement your idea is with AJAX code. AJAX will allow you to call and use your PHP scripts with JavaScript without actually having to navigate the web Browser away to another page. AJAX is what makes possible neat features like the way Google can offer you suggestions as you type them in. It literally creates a bridge between the client side code and server side side code. VERY COOL!
Author: Shalini SIngh | Website URL: |
session_start(); include ("header.php"); include ("navigation.php"); //print_r($_GET); $_SESSION['city']=$_GET['city']; $_SESSION['ocity']=$_GET['ocity']; $_SESSION['state']=$_GET['state']; $_SESSION['zip']=$_GET['zip']; //$_SESSION['zipONe']=$_GET['zipONe']; $_SESSION['country']=$_GET['country']; $_SESSION['radius']=$_GET['radius']; $_SESSION['property_type']=$_GET['property_type']; $_SESSION['min']=$_GET['min']; $_SESSION['max']=$_GET['max']; $_SESSION['numBegin']=$_GET['numBegin']; $_SESSION['begin']=$_GET['begin']; $_SESSION['num']=$_GET['num']; $_SESSION['total']=$_GET['total']; //print_r($_SESSION); include (""); ---------------------------------- now if i want to search through script "" ,it wont send SESSION variables to this script . Any work around for this.
Author: Pavithra | Website URL: |
Please guide me about passing data from one form to another; i collect the details of the user in a form in a html page.When i click on a submit button in this form it ones another form ie html page ("thanks.html") ,here i display a msg "Thank you for registering " along with this msg i want to display the datails of the user entered in the previous form. How appropriately should I use the get and post mtds. Please let me know

Re: From Admin:

Check out this script that I posted online. It will help you with your forms.
Passing info from Form to Form
Author: ashis mohanty | Website URL: |
Question? I want when I click the first button(submit) it will display the total table in the same page and when i click the 2nd one it will display the selected row of the table in another page through catching the id value.
Author: Ayush | Website URL: |
Hey hieee... I just started a project in a hospital to enter medical records of patient. I have given each doctor a username and password. As soon as they log in they can either add a new record or search for an existing.When a doctor searches he gets record of all the patients i.e. patients referred by other doctors too.and they can update the information too. But i want the update to be done only if the patient belongs to that doctor or else other doctors must be only able to view the details but not edit them. Plzzzzz provide your suggestions.... Thanx in advance :
Author: amit | Website URL: |
i have prepared a login form in which when the user enters the user name and password , if the username or password is correct then a new window is displayed but if the username or password is incorrect then the text username or password is inccorect should be displayed at the bottom of the login form and not on the new page
Author: khurshid alam | Website URL: |
I want to pass two three parameter into one link. how i can I do this?

Re: admin

The simplest way to pass parameters with a link is with the $_GET method.


$parameter1 = 'param1test';

$parameter2 = 'param1test';

$parameter3 = 'param1test';


<a href="<?php echo $parameter1 ?>parameter2=<?php echo $parameter2 ?>parameter3=<?php echo $parameter3 ?>&;">This is your link</a?>

Author: neeraj | Website URL: |
I want to pass variable value which is not there in form tag to another page

Re: Admin

I think you are asking me how to pass a variable value that is not inside the form tag on your page.

To understand this scenario we need to understand the concept of forms and links and how they work.

The form tag looks like this:

<form> stuff inside form tag </form>

Inside the form tag will be some text boxes and a submit button that will create variables on the server that you will be able to use on the next page.

<form action="test.php" method="get" >
<input type="text" name="" value="">
<input type="submit" name="" value="">

Now I am assuming that you want to know how you might be able to create variable other than what are found inside the form tag

Well you can add variables coded into a url in the action attribute of the of the form tag

<form action="test.php?newVar=true" method="get" >

If you do this, then you will be able to access "newVar" with $_GET['newVar']

Author: Baharul Islam | Website URL: |
Finally a tutorial where I can learn php. I've been trying to learn php for ever. sending variables from one page to another page is the most important thing to learn in php.

Thank you!
Author: Anil | Website URL: |
I want to pass three value using three hyperlink
Re: Admin
Refer to the section on
Author: Paddy manning | Website URL: |
I was trying to use a php to offer a savable download by passing the name of the file as a variable so a href= download.php?$file = pdfs Economics_in_one_lesson.pdf download a ?php if file_exists $file { header Content-De ion: File Transfer header Content-Type: application octet-stream header Content-Disposition: attachment filename= .basename $file header Content-Transfer-Encoding: binary header Expires: 0 header Cache-Control: must-revalidate, post-check=0, pre-check=0 header Pragma: public header Content-Length: . filesize $file ob_clean flush readfile $file getting an unexpected end error message and am LOST
Author: Paul Rinderer | Website URL: |
echo day=$info[event_day]



Can you help me understand this code.

Thanks in advance
Author: nanda kishore | Website URL: restaurant |
i am working on small website when i am click on search button then call to another page from mysql back end how it is
Author: kumar | Website URL: |
How do i pass a variable to my tpl file if its used like : if $id=$my_id { status=1 } else if condition { status=2 }
Author: shabtu | Website URL: |
Re: Admin
Thank you for your comment Shantu.
Author: Ashwani | Website URL: |
http: test1.php?name=Shaun Lname=Morgan

How can we make above url SEO frinedly on second page.
Re: Admin
what do you mean by seo friendly?
Author: Kevin Blumer | Website URL: |
How do i pass a website to another website so i want to pass to so it comes out like this http: ir that kinda makes sense without the http: looked up a few ways but still dont know how to do it.
Re: Admin
Please email and elaborate on your problem. I'm not sure that I understand exactly.
Author: neha | Website URL: |
session is not working in my pc then?
Author: santosh kumar parbat | Website URL: |
how many variables are able to pass through hyperlink
when i am sendin two variable, its goes easilly. but when sending three variables , than its not going perfecty and showing only value of first two variable. i am using $_GET[ abc ] to retrive value in anather page.
kindly help me. Andd suggest me too.Thankyou
Re: Admin
make sure your variables are coded like :

Author: Rich | Website URL: |
Thanks for the help. My first $_SESSION
Author: JM | Website URL: |
Excellent and very detailed post, I wrote a small tutorial in the same subject, it can be helpful. I want to share it in your blog with your readers. Please take a look here : http: 1339 how-to-send-and-receive-parameters-in-php
Author: Felipe | Website URL: |
Aw, this was an incredibly nice post. Spending some time and actual effort to generate a really good article… but what can I say… I hesitate a whole lot and don t seem to get anything done. this site
Author: Zelma | Website URL: |
Good day Do you use Twitter? I d like to follow you if that would be ok. I m undoubtedly enjoying your blog and look forward to new updates.
Author: Candy | Website URL: |
When someone writes an piece of writing he she keeps the image of a user in his her brain that how a user can know it. So that s why this post is amazing. Thanks
Author: Janina | Website URL: |
It s a shame you don t have a donate button I d without a doubt donate to this superb blog I suppose for now i ll settle for book-marking
and adding your RSS feed to my Google account.
I look forward to brand new updates and will share this site with my Facebook group.
Chat soon
Re: Admin
Thank You
Author: Trent | Website URL: http: |
Thank you for any other informative website.
Author: Alycia | Website URL: |
Normally I do not read article on blogs, however I would like to say that this write-up very compelled me to try and do it Your writing taste has been surprised me. Thank you, quite nice post.
Author: Ofelia | Website URL: |
Heya terrific blog Does running a blog such as this require a large amount of work? I ve very little knowledge of computer programming but I had been hoping to start my own blog in the near future. Anyhow, if you have any recommendations or techniques for new blog owners please share. I understand this is off subject however I simply needed to ask. Appreciate it
Author: Krista | Website URL: |
I leave a response when I especially enjoy a post on a site or I have something to add to the conversation. It s triggered by the fire displayed in the post I browsed. And after this article How To Pass PHP Variable From One Page To Another Page | POST |GET | SESSION. I was actually moved enough to drop a comment :- I actually do have a couple of questions for you if it s okay. Is it simply me or does it seem like a few of the remarks appear as if they are coming from brain dead folks? :-P And, if you are writing on additional online sites, I would like to keep up with you. Could you list the complete urls of all your shared pages like your linkedin profile, Facebook page or twitter feed?
Author: Drew | Website URL: |
Thanks in support of sharing such a good article.
Author: Gabriel | Website URL: |
Hi I know this is kinda off topic but I was wondering which blog platform are you using for this website? I m getting sick and tired of Wordpress because I ve had issues with hackers and I m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.
Author: razgriz | Website URL: |
it helps me alot man.... thankyou so much....
Author: Luis | Website URL: |
Ahaa, its nice dialogue on the topic of this paragraph here at this web site, I have read all that, so now me also commenting here.
Author: Susanne | Website URL: |
I absolutely love your website.. Pleasant colors theme. Did you make this site yourself? Please reply back as I m looking to create my very own blog and would like to find out where you got this from or just what the theme is named. Thanks
Author: Traci | Website URL: |
I usually do not drop a leave a response, but after reading through a few of the responses on this page How To Pass PHP Variable From One Page To Another Page | POST |GET | SESSION. I actually do have a couple of questions for you if it s okay. Is it simply me or do some of these remarks appear as if they are left by brain dead individuals? :-P And, if you are writing on other online sites, I would like to keep up with anything fresh you have to post. Would you make a list of all of all your shared pages like your twitter feed, Facebook page or linkedin profile?
Author: | Website URL: |
Good explanation. It is very helpfull for the beginner......
Re: Admin
thank you
Author: Tod | Website URL: |
Wow that was strange. I just wrote an really long comment but after I clicked submit my comment didn t show up. Grrrr... well I m not writing all that over again. Anyhow, just wanted to say fantastic blog
Author: Rusty | Website URL: |
Howdy This post could not be written any better Reading this post reminds me of my previous room mate He always kept chatting about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing
Author: Brad | Website URL: |
Usually I do not learn post on blogs, however I wish to say that this write-up very forced me to try and do so Your writing taste has been surprised me. Thanks, very great post.
Author: Dewayne | Website URL: |
Greetings Very useful advice in this particular post It s the little changes which will make the largest changes. Thanks a lot for sharing
Author: Tim | Website URL: |
Hello I ve been following your web site for some time now and finally got the bravery to go ahead and give you a shout out from Atascocita Texas Just wanted to say keep up the great job
Author: Christel | Website URL: |
Hey there this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I m starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be enormously appreciated
Author: Seth | Website URL: |
If you are going for most excellent contents like I do, simply pay a visit this site daily as it gives feature contents, thanks
Author: Gertie | Website URL: |
Great blog you ve got here.. It s hard to find excellent writing like yours nowadays. I truly appreciate individuals like you Take care
Author: Alejandra | Website URL:$100/Hour_-_Paid_Jobs |
My coder is trying to convince me to move to . net from PHP. I have always disliked the idea because of the costs. But he s tryiong none the less. I ve been using Movable-type on a number of websites for about a year and am worried about switching to another platform. I have heard great things about Is there a way I can transfer all my wordpress posts into it? Any help would be really appreciated
Author: Sean | Website URL: |
Have you ever thought about including a little bit more than just your articles? I mean, what you say is fundamental and everything. However think of if you added some great pictures or video clips to give your posts more, pop Your content is excellent but with pics and video clips, this website could undeniably be one of the best in its niche. Great blog