Cookie and Session are mentioned at the end of 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 to solve a connection of HTTP ?? SyntaxHighlight
Cookie and Session are mentioned at the end of 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 to solve a connection of HTTP ?? Developed from memory, it can be used to track users or confirm users who return. 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 .?? The website where oso.com.cn is the 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 "), but is this an HTTP header ?? So I do not recommend using this method. it is easier to use setcookie.
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 co ?? Okie 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 ?? Ession to replace 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 environment for PHP scripts ?? Variable. 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 ";
?>