Php sets and reads cookies, and php sets to read cookies.
Concept understanding:
A Cookie is generated by the server and sent to the User-Agent (usually a browser). The browser saves the key/value of the Cookie to a text file in a directory, the Cookie is sent to the server when the same website is requested next time (provided that the browser is set to enable the cookie ).
setcookie(name,value,expire,path,domain,secure)
Set cookie in php
A. Key and value:
setcookie("name",'zhangshan');
B. Set the timeout time:
setcookie("name",'zhangshan',time()+10);
C. Set the path
setcookie("name",'zhangshan',time()+10,'/');
D. Set the access domain
setcookie("name",'zhangshan',time()+10,'mi.com');
D. Set the security access value to 0 or 1.
Note: cookies can only store strings and numbers;
Php reads cookies
A. directly use the key value$ Name
B. Read from the global $ _ COOKIE ['name'] or $ HTTP_COOKIE_VARS ['name ']
Note cookie cannot be set as follows:
setcookie("name",'zhangshan'); setcookie("age",15);
If you want to set multiple values, you can serialize the array and save it to a variable.
$arr = array(1,2,3); $arr_str = serialize($arr); setcookie("name",$arr_str);
Delete cookie in php
setcookie('name');
Or
setcookie('name','zhangsan',time()-1);