Home > Backend Development > PHP Tutorial > php设置和读取cookie

php设置和读取cookie

WBOY
Release: 2016-06-23 13:40:58
Original
1217 people have browsed it


概念理解:

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

setcookie(name,value,expire,path,domain,secure)
Copy after login


php设置cookie

a.键和值:

setcookie("name",'zhangshan');
Copy after login


b.设置超时时间:

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


c.设置路径

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


d.设置访问域

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

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


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


php读取cookie

a.直接用键值 $name

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


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

   setcookie("name",'zhangshan');   setcookie("age",15);
Copy after login

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

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


php删除cookie

setcookie('name'); 
Copy after login

或者

setcookie('name','zhangsan',time()-1); 
Copy after login








Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template