在 PHP cookie 中存储和检索数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19068363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing and retrieving an array in a PHP cookie
提问by 728883902
I'm looking to store some data from some 'virtual' index cards. Each card has a front and a back, and the user can store multiple cards. Each side will have data on it.
我希望从一些“虚拟”索引卡中存储一些数据。每张卡片都有正面和背面,用户可以存放多张卡片。每一方都会有数据。
I ----------------- I I CARD 1 FRONT I I------------------I
I --------------- I I CARD 1 BACK I I-----------------I
I ----------------- I I CARD 2 FRONT I I------------------I
I --------------- I I CARD 2 BACK I I-----------------I
OK, my diagrams got messed up a bit. But you get the message. :)
好吧,我的图表有点乱。但是你会收到消息。:)
Imagine it from the diagrams above. I'd like to store each card's data (front and back) in a cookie, as an array (maybe), and then be able to pull each value back and insert it where applicable (on a different page).
从上图中想象一下。我想将每张卡的数据(正面和背面)存储在一个 cookie 中,作为一个数组(也许),然后能够将每个值拉回来并在适用的地方(在不同的页面上)插入它。
At the same time, bear in mind that the user can make as many cards as they like. I can't use POST or GET functions. The array bit is debatable, if you can think of an easier way of storing this data in a cookie, let me know. Please note: don't suggest storing in a database, as it won't be convenient for the project. :)
同时请记住,用户可以制作任意数量的卡片。我不能使用 POST 或 GET 函数。数组位是有争议的,如果您能想到一种更简单的方法将这些数据存储在 cookie 中,请告诉我。请注意:不建议存储在数据库中,因为它对项目不方便。:)
回答by davidkonrad
Use json_encode
/ json_decode
to get / set arrays in cookies.
使用json_encode
/json_decode
来获取 / 设置 cookie 中的数组。
Test array
测试阵列
$cardArray=array(
'CARD 1'=>array('FRONT I', 'BACK I'),
'CARD 2'=>array('FRONT 2', 'BACK 2')
);
convert and write the cookie
转换并写入cookie
$json = json_encode($cardArray);
setcookie('cards', $json);
the saved string looks like this
保存的字符串看起来像这样
{"CARD 1":["FRONT I","BACK I"],"CARD 2":["FRONT 2","BACK 2"]}
get the cookie back
拿回饼干
$cookie = $_COOKIE['cards'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
show the restored array
显示恢复的数组
echo '<pre>';
print_r($savedCardArray);
echo '</pre>';
outputs
产出
Array
(
[CARD 1] => Array
(
[0] => FRONT I
[1] => BACK I
)
[CARD 2] => Array
(
[0] => FRONT 2
[1] => BACK 2
)
)
Edit
If you wonder about stripslashes
, it is because the string saved actually is
编辑
如果你想知道stripslashes
,那是因为保存的字符串实际上是
{\"CARD 1\":[\"FRONT I\",\"BACK I\"],\"CARD 2\":[\"FRONT 2\",\"BACK 2\"]}
setcookie
adds \
before quoutes to escape them. If you not get rid of those, json_decode
will fail.
setcookie
加上\
之前quoutes逃避它们。如果你不摆脱那些,json_decode
就会失败。
Edit II
编辑二
To add a new card to the cookie
向 cookie 添加新卡
- load the array as above
$savedCardArray['CARD XX']=array('FRONT XX', 'BACK XX');
- save the array as above, but now of course
$savedCardArray
and not$cardArray
.
- 加载数组如上
$savedCardArray['CARD XX']=array('FRONT XX', 'BACK XX');
- 如上所述保存数组,但现在当然
$savedCardArray
不是$cardArray
.
回答by Jules Bartow
Serialize/Unserialize works as a simpler alternative to json_encode / json_decode
序列化/反序列化是 json_encode / json_decode 的更简单替代方案
setcookie('cookiename', serialize(array), ...) to save to cookie.
setcookie('cookiename', serialize(array), ...) 保存到 cookie。
array = unserialize($_COOKIE['cookienam']) to retrieve array.
array = unserialize($_COOKIE['cookienam']) 来检索数组。
回答by Tom
Play with something like this
玩这样的东西
<?php
$card_id = '123';
$value = 'im a black lady';
setcookie("card[$card_id][front]", $value);
// reload page to actually read the cookie
echo $_COOKIE['card'][$card_id]['front']; // im a black lady
?>