PHP syntax (6) _ PHP Tutorial

Source: Internet
Author: User
PHP syntax (6 ). Cookie and Session are mentioned at the end of PHP syntax (3). This article introduces these two technologies. Cookie we often refer to it as "Library Notes", also known as "small dessert". Cookie and Session are mentioned at the end of the previous section ("Talk about PHP syntax (3, this article introduces these two technologies.
Cookie is often referred to as a "database" or "small dessert ". It is a small file stored in the client browser. It is developed to solve a connection of HTTP without memory, and can be used to track users or confirm the returned users. PHP provides the setcookie () function to set the Cookie. Because Cookies are part of the HTTP header. Therefore, the setcookie () function must be called before webpage data is transmitted to the browser. This is the same as calling the header () function.
The Cookie must be provided by the host. Therefore, a cookie header must be sent in the CGI program.

The following example shows how to call the setcookie () function in PHP to set the cookie: Setcookie ("user", "wind", time () + 3600, "/php/", "http://www.oso.com.cn ");
?>

Here, user is the name of the cookie; wind is the value of the cookie; time () + 3600 is the effective time of the cookie;/php/is the relevant path of the cookie; http: // www. oso.com.cn is the website with this cookie.
In fact, in addition to setting cookies in this way, we can also use the header () function such as: header ("Set-cookie: user = wind "), however, this requires an understanding of the HTTP header information, so I do not recommend using this method, or use setcookie () for convenience.
When the browser accesses a website, it automatically checks whether there is a cookie for the website. If yes, it will automatically pass it to the server. in PHP, the return codecomokie is used as a variable. For example, after the cookie is returned, a $ user variable is generated and its value is wind.
However, a fatal drawback of cookies is that if the customer closes the cookie reception and fails to store the cookie to the client, all operations will fail. Therefore, the S extension ession is provided in PHP4 to replace the Cookie.
The biggest difference between Session and Cookie is that Cookie stores information on the client, while Session stores information on the server. In fact, Session provides a global token variable for PHP scripts. Example:
Set a Session named user with the value of wind

Session_start ();
$ User = "wind ";
Session_register ("user ");
?>

Read the Session and the result is "Welcome! Wind"
? Br> Session_start ();
Echo "Welcome! $ User ";
?>


Cookie and Session are mentioned at the end of "Talk about PHP syntax (3)". This article introduces these two technologies. Cookie we often refer to it as "library", also called "small dessert "...

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.