Php from entry to discard series-04. php page value transfer and persistence, php-04.php
Php from entry to discard series-04. php page value transfer and maintain 1. directory structure
2. Transfer value between two pages
You can use get or post to submit a small amount of data between two pages. The difference between the two is not described in detail.
1. get submit
Use get commit to pass data, modify the URL sent to the server in the link address as shown in the following http://www.cnblogs.com/MarkRao/p/php01.html? GName = mark & gAge = 26
Of course, you can also set method = "get" in the form to receive the data value submitted by get in php, and use the predefined $ _ GET variable
The information sent from a form with the GET method is visible to anyone (displayed in the address bar of the browser), and the amount of information sent is limited.
1
The "getPage. php" file can now collect form data using the $ _ GET variable (note that the name of the form field automatically becomes the key in the $ _ GET array ):
1 Welcome <? Php echo $ _ GET ["gName"];?>! <Br> 2. What is your age? <? Php echo $ _ GET ["gAge"];?> Years Old.
2. post submission
Use post submission to pass data, set method = "post" in the form, receive the data value submitted by post in php, and use the predefined $ _ POST variable
The information sent from a form with the POST method is invisible to anyone (not displayed in the address bar of the browser) and there is no limit on the amount of information sent.
Note:However, by default, the maximum number of messages sent by the POST method is 8 MB (you can change it by setting post_max_size in the php. ini file ).
1
The "postPage. php" file can now collect form data using the $ _ POST variable (note that the name of the form field automatically becomes the key in the $ _ POST array ):
1 Welcome <? Php echo $ _ POST ["pName"];?>! <Br> 2. What is your age? <? Php echo $ _ POST ["pAge"];?> Years Old.
3. Maintain values between multiple pages
You can use session or cookie to save data between multiple pages.
1. session data storage
The PHP session variable is used to store information about a user session or change the settings of a user session. Session variables store information of a single user and are available for all pages in the application. The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL.
Before you store user information in a PHP session, you must start the session.
Note:The session_start () function must be located before the
The correct method for storing and retrieving session variables is to use the PHP $ _ SESSION variable:
1 <? Php session_start (); 2 // store session data 3 $ _ SESSION ['viewcount'] = 1; 4?> 5 6
2. cookie-based Data Storage
Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. Each time a computer requests a page through a browser, the computer sends a cookie. With PHP, you can create and retrieve the cookie value.
The setcookie () function is used to set the cookie.
Note:The setcookie () function must be located before the
Syntax:
1 // name key name 2 // value key value 3 // expire storage timeout time 4 // path storage location 5 // domain storage difference domain name 6 setcookie (name, value, expire, path, domain );
$ _ COOKIE variable is used to retrieve the cookie value.
1 <? Php2 // output cookie value 3 echo $ _ COOKIE ["user"]; 4 5 // view all cookie6 print_r ($ _ COOKIE); 7?>
When deleting a cookie, you should change the expiration date to the past time point:
1 <? Php2 // set the cookie expiration time to 3 setcookie ("user", "", time ()-3600) in the past hour; 4?>
OK. This is the end!