Php sets and reads cookies, and php sets to read cookies.

Source: Internet
Author: User
Tags set cookie

Php sets and reads cookies, and php sets to read cookies.


Concept understanding:

A Cookie is generated by the server and sent to the User-Agent (usually a browser). The browser saves the key/value of the Cookie to a text file in a directory, the Cookie is sent to the server when the same website is requested next time (provided that the browser is set to enable the cookie ).

setcookie(name,value,expire,path,domain,secure)


Set cookie in php

A. Key and value:

setcookie("name",'zhangshan');


B. Set the timeout time:

setcookie("name",'zhangshan',time()+10);


C. Set the path

setcookie("name",'zhangshan',time()+10,'/');


D. Set the access domain

 setcookie("name",'zhangshan',time()+10,'mi.com');

D. Set the security access value to 0 or 1.


Note: cookies can only store strings and numbers;


Php reads cookies

A. directly use the key value$ Name

B. Read from the global $ _ COOKIE ['name'] or $ HTTP_COOKIE_VARS ['name ']


Note cookie cannot be set as follows:

   setcookie("name",'zhangshan');   setcookie("age",15);

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

$arr = array(1,2,3);  $arr_str = serialize($arr);    setcookie("name",$arr_str);  


Delete cookie in php

setcookie('name'); 

Or

setcookie('name','zhangsan',time()-1); 








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.