How cookies are used in PHP

Source: Internet
Author: User
Tags setcookie

  This article mainly introduces the use of cookies in PHP, including creating/updating cookies, deleting cookies, fetching cookie values, and so on, which friends can refer to in the following

1. Create/update cookie     code as follows: Setcookie ($cookieName, $value, time () + seconds);     Example: Create a cookie with the name username, The value is ZS, expires in 2 weeks     code as follows: Setcookie ("UserName", "Zs", Time () +2*7*24*3600);     If you do not set the time, it will not be saved to the cookie file. When the browser is not off, it can be accessed. When the browser is closed, it cannot be accessed.   Example:     code is as follows: Setcookie ("Age", "18");     2. Take the value of the cookie     code as follows: $_cookie[$cookieName];     Example: Take out the value of UserName, put in the variable $uname     code as follows: $uName =$_cookie[' UserName '; when the     values It is generally judged whether it is empty, and then take the value operation. The above value-taking operation is generally written as follows:   Code: if (!empty ($_cookie[' UserName ')) {  $uName =$_cookie[' UserName '];  }       3. Delete the specified cookie   code as follows: Setcookie ($cookieName, Value,time ()-seconds);/or Setcookie ($cookiename, "); or Setcookie ($cookiename, NULL);       Example: Delete UserName     code as follows: Setcookie ("UserName", "Zs", Time ()-3600);     4. Delete all cookies for the current session   code as follows: foreach ($_cookie as $key => $val) {Setcookie ($key, "", Time () -100);}     when no CWhen Ookie, the file that saves cookies on this computer will also be deleted.  

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.