Four Methods for passing parameters between PHP pages are described in detail, and four methods are described in detail.

Source: Internet
Author: User

Four Methods for passing parameters between PHP pages are described in detail, and four methods are described in detail.

2016-04-16

Define the page01.php and page02.php PHP files, and try to pass the content in page01 to page02 for our continued use.
--------------------------------------------------------------------------------
First:
Use the cookie of the client browser. Cookie is easy to understand. It is a temporary file that can be viewed as a storage room. The browser records some information during browsing and stores it here temporarily.
Set a cookie in page01.

The Code is as follows:

<? Php setcookie ('mycooker', 'Self-ling');?>

That's simple. We have already created a cookie.
We have defined a variable mycookie whose value is a string 'self-ling '.
We can give the cookie variable a name and define multiple cookie variables.
Accept cookies on the page02 page.

The Code is 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 output is simple.
Now, the cookie is used to pass parameters between pages.
--------------------------------------------------------------------------------
Second:
Use the session on the server. It is easy to understand session. Different from cookie, it is a temporary storage room on the server side. A session is often called a session.
Set a session in page01.

The Code is as follows:

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

To use a session, you must start the session. Session_start (); is the method to start the session. Generally, it should be written at the beginning.
The second statement defines an array of $ _ SESSION ["temp"], which is named $ _ SESSION ["temp"] and contains three strings.
Accept the session on the page02 page.

The Code is as follows:

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

Start the session first. After the startup, the variables defined in page01 can be used and no other operations are required. This is different from cookie.
Next we use the for loop to output its content.
[Do not think $ _ SESSION ['temp '] [$ I] is a two-dimensional array, which is a one-dimensional array named $ _ SESSION ["temp"]. although this name is cumbersome, the subscript of the array is 'temp ']
[When we write $ _ SESSION ["temp"], double quotation marks or single quotation marks are equivalent .]
[Here we define the session variable as an array or a common variable, as mentioned in cookie]
--------------------------------------------------------------------------------
Third:
Use a form for transmission.
Page01.php:

The Code is as follows:

<Form action = "page02.php" method = "post"> <input type = "text" name = "wuziling"/> <input type = "submit" name = "submit" value = "Submit"/> </form>

The attribute action in the form directly specifies the page on which the form content is transmitted. Method indicates the transfer method. Post indicates message transmission, just like sending a text message.
Write page02.php as follows:

The Code is as follows:

<?php      $wu = $_POST['wuziling'];     echo $wu;?>

Use $ _ POST [] to obtain the passed variable value. The variable name wuziling is defined in the name attribute of the input tag of the form.
Then pass it to another variable $ wu. In this way, we can output the data. Direct output is also acceptable, echo $ _ POST ['wuziling'];
[The value of method can also be get]
--------------------------------------------------------------------------------
Fourth:
Use hyperlinks to pass parameters. Many of our online operations involve clicking hyperlinks to jump between webpages. You can also pass parameters.
Page01.php:

The Code is 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 states that you want to jump to the page02 page. Add a question mark, a new variable defined by yourself [this name will be used on the page02 page], and the value of new is the $ var we want to pass.
Write page02.php as follows:

The Code is as follows:

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

Use $ _ GET [] to obtain the value of new, and then output or use it for other purposes.
In this case, the new variable and its value are directly displayed in the browser address bar.

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.