Phpsetcookie () function description

Source: Internet
Author: User
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
  1. Setcookie ("TestCookie", 9 streets, time () + 3600, "/", ".9streets.cn", 1 );

Set the cookie to be valid on the current day
 

PHP code
  1. // Set the default time zone
  2. $ Timezone = "Asia/Shanghai ";//Date_default_timezone_set () 
  3. If (PHP_VERSION> = '5. 1 '&&! Emptyempty ($ timezone ))
  4. {
  5. Date_default_timezone_set ($ timezone );
  6. }
  7. // Obtain the unix timestamp at tomorrow
  8. $ Nonce_time = strtotime (date ('ymmd') + 1 );
  9. // Subtract the remaining time from the expiration date
  10. $ Expire_time = $ nonce_time-time ();
  11. // Echo $ expire_time;
  12. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.