Home >Backend Development >PHP Tutorial >A brief discussion of PHP syntax (6)_PHP tutorial
Cookie and Session are mentioned at the end of the above article ("Talk about PHP Grammar (3)". This article will give some introduction to these two technologies.
We often pronounce Cookie as "Kuji", also Call it a "little dessert". It is a small file stored in the client's browser. It was developed to solve the problem of HTTP connection without memory and can be used to track users or respond to returns. The user confirms. PHP provides the setcookie() function to set cookies. Because cookies are part of the HTTP header, the setcookie() function must be called before the web page data is sent to the browser. () function is the same.
Cookie must be provided by the host; therefore, we must send a header setting the cookie in the CGI program.
Among them, user is the name of the cookie; wind is the value of the cookie; time()+3600 is the validity time of the cookie; /php/ is the relevant path of the cookie; http://www.kunoso. com.cn is the website for this cookie.
In fact, in addition to using this method to set cookies, we can also use the header() function such as: header("Set-Cookie:user=wind"), but this requires understanding the HTTP header information. You only need to understand it, so the author does not recommend using this method. It is more convenient to use setcookie().
When reading cookies, when the browser connects to a certain website, it will automatically check whether there are cookies from the website. If there are cookies, it will be automatically passed to the server. In PHP, the cookie returned will be okie as a variable. After the cookie set above is returned, a $user variable will be formed, whose value is wind.
However, cookies have a fatal flaw, that is, if the customer turns off cookie reception and cannot store cookies to the client, all operations will go wrong. Therefore, Session is provided in PHP4 to replace Cookie.
The biggest difference between Session and Cookie is that Cookie stores information on the client side, while Session stores it on the server side. In fact, Session provides a global variable for PHP scripts. An example is as follows:
Set a Session named user with the value wind