Summary of several methods for passing parameter values on the php page

Source: Internet
Author: User

Php is a script language for servers. It is also the most popular WEB development language, the following describes several methods for passing parameters between four pages that are commonly used in php development applications.

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: Copy code
<? Php
Setcookie ("VisitTimes", $ VisitTimes, time () + 31536000 );
?>

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: Copy code
<?
$ HTTP_COOKIE_VARS ["VisitTimes"]? ($ VisitTimes ++) :( $ VisitTimes = 1 );
 
Echo "<B> welcome <font color =" # FF0000 ">". $ VisitTimes.
"</Font> visit my homepage </B> <br> n ";
?>

For more details, see http://www.bKjia. c0m/phper/php-gj/33355.htm

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. Session is often called a session.
Set a session in page01.

The Code is as follows: Copy code
<? Php
Session_start ();
$ _ SESSION ["temp"] = array ('20160301', '20160301', '20160301 ');
?>

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: Copy code
<? 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.
_ Post: it can only receive data when php obtains the method = "post" of the form. The following code

The Code is as follows: Copy code

<Form id = "form1" name = "form1" method = "get" action = "">
<Label>
<Input type = "text" name = "cn" value = 'Get Me'/>
</Label>
</Form> a. php page

<?
If ($ _ post)
{
Echo $ _ post ['cn'];
}
Else
{
Echo 'no value obtained ';
}
?>

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: Copy code
<? Php
$ Var = 'I love you! ';
?>
<A href = "http://www.bKjia. c0m <? 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: Copy code
<? Php
Echo $ _ GET ['new'];
?>

Use $ _ GET [] to obtain the value of new, and then output or use it for other purposes.

Note:The http get method is not suitable for large variable values. The value cannot exceed 100 characters.
$ _ Request variable
The $ _ request variable in php contains $ _ get, $ _ post, and $ _ cookie content.

The $ _ request variable of php can be used to obtain the results of form data sent through the get and post methods.

Example
Welcome <? Php echo $ _ request ["name"];?>. <Br/>
You are <? Php echo $ _ request ["age"];?> Years old!

Summary:

We have discussed four methods for passing parameters on the page: session, cookie, post, and get. There are probably so many other programming languages. post, get is basically used for passing parameters and page cookies in forms and URLs. session stores them in a Global File or variable.

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.