Methods for passing values and holding values between pages in PHP _php instance

Source: Internet
Author: User
Tags php session send cookies set cookie setcookie unique id

First, directory structure

Passing values between pages in two or two times

By passing small amounts of data between pages two, you can use get commits, or post submissions, and the differences are not.

1, get submit

Use get commit to pass the data, modify the URL sent to the server in the link address below http://www.cnblogs.com/MarkRao/p/php01.html?gName=mark&gAge= 26, of course, you can also set method= "get" in the form, PHP to receive the data submitted over the value, using predefined $_get variables

The information sent from a form with a Get method is visible to anyone (it appears in the browser's address bar), and there is a limit to the amount of information sent.

 
 

The "getpage.php" file can now collect form data by $_get variables (note that the name of the form field automatically becomes the key in the $_get array):

1 Welcome <?php Echo $_get["Gname"];?>!<br>
2 Your age is <?php echo $_get["GAge"]?> years old.

2, Post submission

Use post submit to pass data, set method= "Post" in the form, receive post submitted data value in PHP, use predefined $_post variable

Information sent from a form with a POST method is not visible to anyone (not shown in the browser's address bar), and there is no limit to the amount of information sent.

Note: However, by default, the maximum amount of information sent by the POST method is 8 MB (can be changed by setting the post_max_size in the php.ini file).

 
 

The "postpage.php" file can now collect form data by $_post variables (note that the name of the form field automatically becomes the key in the $_post array):

Welcome <?php Echo $_post["PName"];?>!<br>

Maintain values between pages of three and more

Keep the data between multiple pages, you can save it with a session, or you can use cookies to save the difference.

1. Save data in session

The PHP session variable is used to store information about the user's sessions, or to change the settings for a user's session. The session variable stores information for a single user and is available for all pages in the application. The session works by creating a unique ID (UID) for each visitor and storing variables based on this UID. The UID is stored in a cookie or transmitted through a URL.

Before you store the user information in the PHP session, you must first start the conversation.

Note: the Session_Start () function must precede the

The correct way to store and retrieve session variables is to use PHP's $_session variable:

<?php session_start ();
Store session Data
$_session[' Viewcount ']=1; 
? >
 
 

2. Cookies Save Data

Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. This computer will send cookies whenever the same computer requests a page through a browser. With PHP, you can create and retrieve the value of a cookie.

The Setcookie () function is used to set cookies.

Note: the Setcookie () function must precede the

The syntax is as follows

The name store's key name//value the stored
key value//expire the stored
timeout//path the stored
location
//domain store the distinguished domain name
setcookie (name, Value, expire, path, domain);

The $_cookie variable is used to retrieve the value of the COOKIE.

<?php
//Output cookie value
echo $_cookie["user"]; 
View all Cookie
print_r ($_cookie);
? >

When you delete a cookie, you should change the expiration date to a past point in time:

<?php
//Set cookie expiration to last 1 hours
setcookie ("User", "", Time () -3600);
? >

The above is a small set to introduce the PHP page between the values and values to maintain the method, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.