Home  >  Article  >  Backend Development  >  php设置和读取cookie

php设置和读取cookie

WBOY
WBOYOriginal
2016-06-23 13:40:581195browse


概念理解:

Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是浏览器设置为启用cookie)。

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


php设置cookie

a.键和值:

setcookie("name",'zhangshan');


b.设置超时时间:

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


c.设置路径

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


d.设置访问域

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

d.设置安全性访问值0或1


注:cookie只能存储字符串、和数字;


php读取cookie

a.直接用键值 $name

b.超全局$_COOKIE['name']或$HTTP_COOKIE_VARS['name']读取


注cookie不能如下设置多个值:

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

如果要设置多个值,可以序列化数组,然后再存到某个变量

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


php删除cookie

setcookie('name'); 

或者

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








Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn