Four ways to pass parameters between PHP pages _php tips

Source: Internet
Author: User
Tags php and
We define the page01.php and page02.php two PHP files, we will try to transfer the contents of PAGE01 to PAGE02, and then let us continue to use.
--------------------------------------------------------------------------------
First Type:
Use a cookie from the client browser. Cookies are easy to understand, is a temporary file, you can see it as a storage room, the browser in the process of browsing the record of some information, temporarily stored here.
Set a cookie in the page01.
Copy Code code as follows:

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

It is so simple that we have created the cookie complete.
We define a variable MyCookie, and its value is the string ' self-psychic '.
We can simply name the cookie variable, and we can define multiple cookie variables.
Accept cookies on the Page02 page.
Copy Code code as follows:

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

We use $_cookie[] to extract the variable MyCookie from the COOKIE and pay its value to $wuziling. And then the simple output.
All right, here we go. Use cookies to pass arguments between pages.
--------------------------------------------------------------------------------
The second type:
Use server-side session. Understanding the session is a very easy thing to do. The difference with a cookie is that it is a temporary storeroom on the server side. Sessions are often called conversations.
Set a session in PAGE01.
Copy Code code as follows:

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

To use session, you must start the session. Session_Start () is the way to start the session. Usually write at the front.
The second statement defines a $_session["temp" array whose name is $_session["Temp", which stores 3 strings.
Accept the session on the PAGE02 page.
Copy 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 PAGE01 are already available after startup, and there is no need for any other fetch operations, unlike cookies.
Below we use a for loop to output its content.
"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 array subscript is ' temp '"
"When we write $_session[" temp ", the temp plus double quotes or single quotes are equivalent. 】
"Here we define the session variable as an array, or we can define a generic variable, as the cookie says."
--------------------------------------------------------------------------------
The third type:
Use forms to pass.
page01.php wrote this:
Copy Code code as follows:

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

The property action within the form specifies to which page the form content is passed directly. Method indicates how it is passed. Post representatives use message delivery, just as we send text messages.
Page02.php wrote this:
Copy Code code as follows:

<?php
$wu = $_post[' wuziling '];
Echo $wu;
?>

Use $_post[] to get the value of the variable passed over. This variable name wuziling is defined in the name attribute of the input label for 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 do not understand, please refer to this section of another detailed introduction to the form submitted by the post"
"The value of method can also be get"
--------------------------------------------------------------------------------
The Fourth kind:
Pass parameters by using hyperlinks. Many of the things we do on the Internet are clicking hyperlinks to jump between the pages. Point can also be passed as a parameter.
page01.php wrote this:
Copy Code code as follows:

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

Define a variable $var.
The href attribute of hyperlink A is stated to jump to the PAGE02 page. Add a question mark later, a variable of your own definition "this name is used on PAGE02 page", the value of new is the $var we want to pass.
Page02.php wrote this:
Copy Code code as follows:

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

use $_get[] to get the value of new, and then it can be exported or used for other purposes.
Then the browser address bar can directly see the new variable and its value.

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.