How to use cookies in php. 1. the code used to create and update a cookie copy is as follows: setCookie ($ cookieName, $ value, time () + seconds); example: Create a cookie named UserName and value zs, the expiration time is 2 weeks. create/update cookie
The code is as follows:
SetCookie ($ cookieName, $ value, time () + seconds );
Example: create a cookie named UserName with a value of zs and an expiration time of 2 weeks.
The code is as follows:
Setcookie ("UserName", "zs", time () + 2*7*24*3600 );
If no time is set, it is not saved to the cookie file. When the browser is not closed, it can be accessed. When the browser is closed, it cannot be accessed.
Example:
The code is as follows:
Setcookie ("Age", "18 ");
2. retrieve the cookie value
The code is as follows:
$ _ Cookie [$ cookieName];
Example: extract the UserName value and put it in the variable $ uName.
The code is as follows:
$ UName = $ _ COOKIE ['username'];
When the value is set, it is generally determined whether it is null before the value operation. The preceding values are generally written as follows:
The code is as follows:
If (! Empty ($ _ COOKIE ['username'])
{
$ UName = $ _ COOKIE ['username'];
}
3. delete the specified cookie
The code is as follows:
Setcookie ($ cookieName, value, time ()-seconds );
// Or
Setcookie ($ cookiename ,'');
// Or
Setcookie ($ cookiename, NULL );
Example: delete UserName
The code is as follows:
Setcookie ("UserName", "zs", time ()-3600 );
4. delete all cookies of the current session
The code is as follows:
Foreach ($ _ COOKIE as $ key => $ val ){
Setcookie ($ key, "", time ()-100 );
}
When no cookie exists, the files that save the cookie on the local machine will also be deleted.
The signature code is as follows: setCookie ($ cookieName, $ value, time () + seconds); example: Create a cookie named UserName and its value is zs, the expiration time is 2 weeks...