Let's talk about how to use setcookie today and how it is about cookie expiration. let's talk about it in detail.
Let's talk about how to use setcookie today and how it is about cookie expiration. let's talk about it in detail.
Setcookie instance:
PHP code
- Setcookie ("TestCookie", 9 streets, time () + 3600, "/", ".9streets.cn", 1 );
Set the cookie to be valid on the current day
PHP code
- // Set the default time zone
- $ Timezone = "Asia/Shanghai ";//Date_default_timezone_set ()
- If (PHP_VERSION> = '5. 1 '&&! Emptyempty ($ timezone ))
- {
- Date_default_timezone_set ($ timezone );
- }
- // Obtain the unix timestamp at tomorrow
- $ Nonce_time = strtotime (date ('ymmd') + 1 );
- // Subtract the remaining time from the expiration date
- $ Expire_time = $ nonce_time-time ();
- // Echo $ expire_time;
- Setcookie ('test', 'validity period is the day', $ expire_time );
Syntax
Setcookie (name, value, expire, path, domain, secure)
PHP setcookie () usage
Parameters |
Description |
Example |
Name |
Name of the required Cookie |
Use $ _ COOKIE ['cookiename'] to call a cookie named cookiename |
Value |
Required Cookie value |
This value is stored on the client and cannot be used to save sensitive data. If the name is 'cookiename', you can use $ _ COOKIE ['cookiename'] to obtain its value. |
Expire |
Optional Cookie expiration time |
This is a Unix timestamp, that is, the number of seconds from the Unix epoch. In other words, the time () function and the number of seconds are usually used to set the cookie failure period. Or use mktime. Time () + 60*60*24*30 will set the cookie to expire after 30 days. If not set, the cookie will expire after the session ends (usually closed by the browser ). |
Path |
Optional valid Cookie path on the server |
If this parameter is set to '/', the cookie is valid throughout the domain. if it is set to '/foo /', the cookie is valid only in the/foo/directory under the domain and its subdirectories, for example,/foo/bar /. The default value is the current directory of the cookie. |
Domain |
Optional domain name valid for this cookie |
To make the cookie valid for all subdomains under the example.com domain name, set this parameter to '.example.com '. Although it is not necessary, it will be compatible with more browsers. If this parameter is set to www.example.com, it is valid only in the www subdomain. For details, see tail matching in Cookie specifications. |
Secure |
Optional. indicates whether the cookie is transmitted only through a secure HTTPS connection. When set to TRUE, the cookie is only set in secure connections. The default value is FALSE. |