Home > Backend Development > PHP Tutorial > PHP Cookie中保存数组

PHP Cookie中保存数组

WBOY
Release: 2016-06-20 13:04:26
Original
2289 people have browsed it

PHP Cookie中保存数组

cookie就是一串字符串,不能存储数组这种东西。所以cookie默认是不能存数组的,下面的写法是错误的。

$arr = array(1,2,3);<br />setcookie('a',$arr);
Copy after login


报错如下:Warning: setcookie() expects parameter 2 to be string, array given in

在PHP里面实现cookie存数组的方法如下:

先用serialize序列化数组,再存入COOKIE ,读出来时用unserialize得到原来的数组
//存入

$arr = array(1,2,3);<br />$arr_str = serialize($arr);<br />setcookie("a",$arr_str);
Copy after login


//取出

$arr_str = $_COOKIE['a'];<br />$arr = unserialize($arr_str);
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