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 "...