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

PHP Cookie中保存数组

WBOY
WBOYOriginal
2016-06-20 13:04:262297browse

PHP Cookie中保存数组

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

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


报错如下: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);


//取出

$arr_str = $_COOKIE['a'];<br />$arr = unserialize($arr_str);




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