PHP Cookie
Cookies are often used to identify users.
What are Cookies?
Cookie is generated by the server and sent to User-Agent (usually a browser). The browser will save the key/value of the Cookie to a text file in a certain directory. Next time This cookie is sent to the server when the same website is requested (provided that the browser is set to enable cookies). Cookie names and values can be defined by the server-side developer, so that the server can know whether the user is a legitimate user and whether he needs to log in again, etc. The server can set or read the information contained in Cookies to maintain the status of the user's session with the server. .
Notes:
1. Different computers cannot share Cookies
2. Different browsers on the same computer cannot share Cookies
3. Cookies cannot be shared under the same browser and different domain names
4. Even under the same browser and the same domain name, cookies with different paths cannot be shared
How to create cookies?
Create a cookie through the setcookie() function, returning TRUE if successful, otherwise returning FALSE.
Note: The setcookie() function must be located before the tag.
Syntax
setcookie(name, value, expire, path, domain);
Parameter description:
ParametersDescription
name Cookie name
value Optional, cookie value
expire Optional, expiration time, timestamp format
path Optional, server-side valid path, / indicates that the entire domain name is valid, the default is the path of the page when the cookie is currently set
domain Optional, Valid domain name of this cookie
Example 1
In the following example, we will create a cookie named "user" and assign it the value "php". We also specify that this cookie expires after one hour:
. When using a cookie, the cookie value will be automatically URL-encoded and automatically decoded when retrieved.
Example 2
How to retrieve the value of Cookie?
PHP’s $_COOKIE variable is used to retrieve the value of the cookie.
// Output cookie value
echo $_COOKIE["user"];
// View all cookies
print_r($ _COOKIE);
?>
In the following example, we use the isset() function to confirm whether the cookie has been set:
php中文网(php.cn) "; else echo "普通访客!
"; ?>
How to delete cookies?
You can delete a cookie by setting the cookie expiration time to a previous time point:
Deleted example:
Tips:
Due to protocol restrictions, no content can be output to the browser before the cookie is set.
The cookie will not take effect on the current page set. To access the set cookie, another page must be visited before it expires
Since cookie information is stored in the user's computer, it is possible to forge cookies and cause cookie fraud. Generally, the cookie value can be encrypted to prevent fraud. When reading cookies, just decrypt the cookies
What should I do if the browser does not support cookies?
If your application needs to deal with browsers that do not support cookies, then you will have to use other methods to pass information between pages in your application. One way is to pass data through a form (forms and user input are covered in the previous chapters of this tutorial).
The following form submits user input to "welcome.php" when the user clicks the "Submit" button:
php中文网(php.cn)
Retrieve the value in the "welcome.php" file, as follows Shown:
php中文网(php.cn) 欢迎 .
你 岁了。
Cookie restriction issue
Many browsers have limits on the number of cookies. Most browsers stipulate the number of cookies that a website can set. It cannot exceed 50, and some browsers even limit it to 30
Browsers also have restrictions on the size of Cookies, which generally cannot exceed 4K in size
Cookie Security Issues
If you do not shut down your computer after surfing the Internet in an Internet cafe, other people who use your computer can view the history of all the websites you visit and the cookie content saved by the website. If the important data (user name , password, card number, mobile phone number, ID number...) saving in cookies is a very dangerous behavior.
Therefore, important data cannot be stored in Cookies. If it must be saved, it must rely on the server