PHP implementation of the method of passing values and preserving values between pages

Source: Internet
Author: User
This article mainly describes the PHP page transfer values and hold the value of the method, the transfer value is mainly through get and post submission, through session and cookie to maintain the data, this article introduces very detailed, with reference value, the need for friends can refer to the next

I. Directory structure

Two, two times between the page transfer values

A small amount of data is passed between two pages, either by a get commit or by using post, the difference is not to be mentioned.

1. Get Submit

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

Information sent from a form with a GET method is visible to anyone (displayed in the browser's address bar) and has a limit on the amount of information sent.


The "getpage.php" file can now collect form data through 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 your age is <?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 of post submission in PHP, use predefined $_post variable

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

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


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

Welcome to <?php Echo $_post["PName";?>!<br> your age is <?php echo $_post["page"];?> years old.

Hold values between multiple pages

Keep data between multiple pages, either by using the session or by using a cookie, the difference is not to be mentioned.

1. Session Save data

The PHP session variable is used to store information about user sessions (session) or to change settings for user sessions. 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 the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.

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

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 a server leaves on a user's computer. This computer sends a cookie whenever a page is requested by the same computer 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

Name stores the key name//value stores the key value//expire the stored time-out time//path the stored location//domain stores 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 Cookieprint_r ($_cookie);? >

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

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

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.