PHP set up and read cookies

Source: Internet
Author: User
PHP settings and read cookies


Concept Understanding:

Cookies are generated by the server and sent to User-agent (typically a browser), and the browser saves the key/value of the cookie to a text file in a directory, The next time you request the same Web site, the cookie is sent to the server (provided the browser is set to enable cookies).

Setcookie (Name,value,expire,path,domain,secure)


PHP Settings Cookie

A. Key and value:

Setcookie ("name", ' Zhangshan ');


B. Set the timeout period:

Setcookie ("name", ' Zhangshan ', Time () +10);


C. Setting the path

Setcookie ("name", ' Zhangshan ', Time () +10, '/');


D. Setting the Access domain

Setcookie ("name", ' Zhangshan ', Time () +10, ' mi.com ');

E. Setting security access values 0 or 1


Note: Cookies can only store strings, and numbers;


PHP Read cookies

A. Direct use of key values $name

B. Hyper global $_cookie[' name '] or $http_cookie_vars[' name ') read


Note A cookie cannot set multiple values as follows:

   Setcookie ("name", ' Zhangshan ');   Setcookie ("Age", 15);

If you want to set multiple values, you can serialize the array and then save to a variable

$arr = Array (+/-);  $arr _str = serialize ($arr);    Setcookie ("name", $arr _str);  


PHP Delete Cookies

Or









  • Related Article

    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.