Four ways to pass parameters between PHP pages

Source: Internet
Author: User
This article is to the PHP page parameter passing four kinds of methods to carry on the detailed analysis introduction, needs the friend reference under

We define two PHP files for page01.php and page02.php, and we will try to pass the contents of the PAGE01 to PAGE02 and then let us continue to use them.
--------------------------------------------------------------------------------
The first type:
Use a cookie from the client browser. Cookie is easy to understand, is a temporary file, you can think of it as a storage room, the browser in the process of browsing to record some information, it is temporarily stored here.
Set a cookie in the page01.

Copy the Code code as follows:

<?php        Setcookie (' MyCookie ', ' self-spirit '); >


It is so simple that we have created a cookie complete.
We define a variable MyCookie, whose value is the string ' self-spirit '.
We can name the cookie variable arbitrarily, and we can define multiple cookie variables.
Accept cookies on the Page02 page.

Copy the Code code as follows:

<?php     $wuziling = $_cookie[' MyCookie ');     Echo $wuziling;? >

We use $_cookie[] to extract the variable MyCookie in the COOKIE and pay its value to $wuziling. Then the simple output.
Okay, here we go. Use a cookie to pass parameters between pages.
--------------------------------------------------------------------------------
The second type:
Use the server-side session. It's easy to understand the session. Unlike a cookie, it is a temporary storage room on the server side. The session is often referred to as a conversation.
Set a session in the PAGE01.

Copy the Code code as follows:

<?php session_start (); $_session["Temp"]=array (' 123 ', ' 456 ', ' 789 ');? >


To use the session, you must start the session. Session_Start (); Is the way to start the session. Usually written in the front.
The second statement I defined a $_session["temp"] array whose name is $_session["Temp", which stores 3 strings.
Accept the session on the PAGE02 page.

Copy the Code code as follows:

<?php      session_start ();     for ($i =0; $i <3; $i + +)     {             echo $_session[' temp '] [$i]. ' <br/> ';     }? >


Start the session first. The variables we defined in the PAGE01 are already ready to use, and do not require any other acquired actions, unlike cookies.
Below we use a for loop to output its contents.
"Do not think $_session[' temp ' [$i] is a two-dimensional array, it is a one-dimensional array, the name of the array is $_session[" temp "], although this name is more cumbersome, the subscript of the array is ' temp '"
"When we write $_session[" temp ", temp plus double quotes or single quotes is equivalent. 】
"Here we define the session variable as an array, or we can define a normal variable, as the cookie says."
--------------------------------------------------------------------------------
The third type:
Use the form to pass.
Page01.php writes like this:

Copy the Code code as follows:

<form action= "page02.php" method= "POST" >     <input type= "text" name= "wuziling"/>     <input type= " Submit "Name=" Submission "value="/></form>

The properties within the form action directly specify which page this form content is passed to. Method indicates the way of delivery. The post represents the use of messaging, just as we send text messages.
Page02.php writes like this:

Copy the Code code as follows:


Use $_post[] to get the value of the variable passed in. The variable name wuziling is defined in the name attribute of the input tag of the form.
It is then passed to another variable, $WU. So we can output it. Direct output is also possible, echo $_post[' wuziling '];
"If you don't understand, please refer to another post in this section detailing the form submission"
"The value of method can also be a get"
--------------------------------------------------------------------------------
The fourth type:
Use hyperlinks to pass parameters. Many of the things we do on the Internet are click-through hyperlinks to jump between pages. Points can also be passed as parameters.
Page01.php writes like this:

Copy the Code code as follows:


<?php
$var = ' I love you! ';
?>
<a href= "<?php echo" page02.php?new= ". $var?>" >get</a>


Defines a variable $var.
The href attribute of hyperlink A is indicated to jump to the PAGE02 page. Add a question mark, a self-defined variable new "this name is to be used on the PAGE02 page", and the new value is the $var we want to pass.
Page02.php writes like this:

Copy the Code code as follows:


<?php
echo $_get[' new '];
?>


use $_get[] to get the value of new and then output or do something else.
At this point the browser address bar can see the new variable and its value directly.

Related recommendations:

PHP page Static example of the detailed

PHP page Set cache time instance code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.