PHP cookie processing function o ☆[ BINGO!]
OK. Let's first look at what cookie is?
Cookie is a small file that the server stays on the client to identify users or store some data (note that session is stored on the server side, which is one of the differences between the two ). When logging on to a portal, we usually have the option "save login" or "automatically log on next time". When we select this option, the server will create a cookie file in our browser to save our information. When a computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve the cookie value. Cookie is a very important role in the web. It was generated as early as the web browser. Cookies are often used for user verification systems.
1. Create a cookie
The setcookie () function can generate a cookie in PHP. Because the cookie is the content of the HTTP header, you must call setcookie () before outputting any data. This restriction is similar to header () and is defined:
1 bool setcookie (string name [, string value] [, int expire] [, string path] [, string domain] [, bool secure] [, bool httponly])
Many parameters! A is not urgent. Let's take a look at the functions of each parameter:
Name: required, indicating the cookie name.
Value: Optional. It indicates the cookie value, which is stored on the client. If it is null, it indicates that the cookie information in the client is revoked (so that the cookie can be deleted ).
Expire: (optional) indicates the effective end time of the cookie, that is, the expiration time. If it is not specified or it is specified as 0, it is usually disabled when the browser is closed.
Path: Optional. Valid cookie path.
Domain: (optional) Valid cookie domain name.
Secure: Valid only during secure HTTPS transmission.
Instance (create a cookie named test with the value of China ):
1
The value given by setcookie () can only be a number or string, and cannot be other complex structures.
2. Get cookie
After creating a cookie, you can use the predefined variable $ _ COOKIE to obtain the cookie. However, the cookie can only be obtained on other pages, because in php, the configured cookie does not take effect on this page unless the page is refreshed.
Instance:
1
The reason for refreshing the page is that the cookie value is not stored in the $ _ COOKIE variable immediately after setcookie () is called, but is sent to the server as the http request is sent, at this time, the cookie value will appear in $ _ COOKIE.
3. cookie Validity Period
A cookie has a lifecycle, that is, the validity period of the cookie. You can set the third parameter to set the validity period.
Instance (several methods for setting the cookie validity period ):
1 setcookie ("cookie_one", "A", time () + 60*60); // The cookie expires after one hour. 2 setcookie ("cookie_two", "B ", time () + 60*60*24); // The cookie expires after one day. 3 setcookie ("cookie_three", "C", mktime )); // The cookie expires at, January 1, October 9, 2020. 4 setcookie ("cookie_four", "D"); // The cookie becomes invalid after the browser is disabled.
4. Valid cookie Path
The path in the cookie is used to control the path under which the set cookie is valid. The default value is '/'. It is valid in all paths, that is, it is valid in the whole server domain name, when other paths are set, they are only valid under the specified path and sub-path, for example:
1 setcookie('test', time(), 0, '/path');
The above settings will make test valid in/path and sub-path/abc, but the cookie value of test cannot be read in the root directory.
Generally, all paths are used. The path is set only when there are very few special requirements. In this case, the cookie value is passed only in the specified path, it can save data transmission, enhance security and improve performance.
5. Delete cookies
Deleting a cookie is simple. It is also implemented through setcookie () (do not use unset ()!!!) The following code is a simple example:
1 setcookie("test","");
You can delete a cookie by setting the second parameter to null. If a specific value is provided for the cookie, you still need to provide these parameters when deleting the cookie so that PHP can correctly Delete the cookie.