Home >Backend Development >PHP Tutorial >Basic use of cookies in php
1. Cookie Introduction
Cookie
is a mechanism that stores data on the remote browser side and uses it to track and identify users. Cookie
is completely maintained on the client, such as: IE
firefox
When the client disables cookie
, it will no longer be used.
2.Cookie configuration and application
Setcookie(string name, string value, int expire,string path, string domain, int secure);
name
is the cookie variable name identifier. You can use it to reference cookie variables in PHP just like using ordinary variable names; (required)
value
is cookie The initial value of the variable; (required)
expire
Indicates the validity time of the cookie variable; (optional.)
path
The relative path of the cookie variable; (Optional.)
domain
Represents the website of the cookie variable; (Optional .)
secure
is valid only when https
is securely transmitted. (Optional.)
3. Receive and process cookies
<?php $value = "my cookie value";// 发送一个简单的 cookie setcookie("TestCookie",$value); ?> ... <html> <body>
echo $TestCookie; echo $CookieArray[0]; echo $_COOKIE["TestCookie"]; echo $HTTP_COOKIE_VARS["TestCookie"];
4. Delete cookies
To delete an existing Cookie
, there are two ways:
SetCookie("TestCookie", "");
SetCookie("TestCookie", "value" , time()-1 / time() );
##5. Restrictions on using cookies
HTML file is output;
Cookie's handling is inconsistent and sometimes produces erroneous results. The limit is on the client side.
Cookie that can be created by a browser is 30, and each cannot exceed 4KB. Each
WEB site can set The total number of cookies cannot exceed 20.
The above is the detailed content of Basic use of cookies in php. For more information, please follow other related articles on the PHP Chinese website!