The syntax format of the setcookie() function is as follows:
bool setcookie(string name[,string value[,int expire[,string path[,string domain[,int secure]]]]]);
The parameter description is as follows:
name Cookie name
value Cookie value
expire Cookie expiration time
path The valid path of the cookie on the server side
domain The valid domain name of the cookie
source Indicates whether the cookie is passed through secure HTTPS
Set the cookie
setcookie('name', 'tom', time()+60, '/', '', false);
Get the cookie
echo $_COOKIE['name'];
Delete cookies
setcookie('name', 'tom', time()-60, '/', '', false);
Set cookie array
Method one:
setcookie('profile[name]', 'zhangsan'); setcookie('profile[gender]', 'male'); setcookie('profile[age]', 24); foreach($_COOKIE['profile'] as $k=>$v) { echo $k.':'.$v; }
Method two:
$arr = array( 'name'=>'tom', 'gender'=>'male', 'age'=>28 ); $serArr = serialize($arr); setcookie('intro', $serArr, time()+60); print_r( unserialize($_COOKIE['intro']) );
Recommended learning: 《PHP Video Tutorial》