存储和检索在PHP中的cookie数组 [英] Storing and retrieving an array in a PHP cookie
问题描述
我在找一些虚拟索引卡存储一些数据。每个卡具有正面和背面,并且用户能够存储多张卡。每一方都会有它的数据。
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. :)
从上面的图中想象它。我想每个卡的数据(正面和背面)存储在cookie中,作为数组(也许),然后就可以拉每个值回并插入适用(在不同的页面)。与此同时,铭记,因为他们喜欢的用户可以尽可能多的卡。我不能使用POST或GET功能。该阵列位是有争议的,如果你能想到的在cookie中存储这些数据更简单的方法,让我知道。请注意:不建议存储在数据库中,因为它不会便于项目。 :)
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). 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. :)
我14岁,和PHP是不是我的最强的语言,所以请保持简单。如果你考虑下投票,关闭的问题,等等。我只用那么作为最后的手段,请记住这一点,我已经研究这个话题时,无法找到答案。我也有一个查找类似矿的问题,找不到任何太相似了。 :)
I'm 14 years old, and PHP isn't my strongest language, so please keep it simple. Please bear this in mind if you consider down voting, closing the question, etc. I only ever use SO as a last resort, and I have researched this topic for hours and could not find an answer. I've also had a look for a question similar to mine, and couldn't find any too similar. :)
推荐答案
使用 json_en code
/ json_de code
来的get / set阵列中的cookies。
Use json_encode
/ json_decode
to get / set arrays in cookies.
测试阵列
$cardArray=array(
'CARD 1'=>array('FRONT I', 'BACK I'),
'CARD 2'=>array('FRONT 2', 'BACK 2')
);
转换和写入cookie的
convert and write the 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"]}
得到的cookie回来
get the cookie back
$cookie = $_COOKIE['cards'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
显示恢复阵列
echo '<pre>';
print_r($savedCardArray);
echo '</pre>';
输出
Array
(
[CARD 1] => Array
(
[0] => FRONT I
[1] => BACK I
)
[CARD 2] => Array
(
[0] => FRONT 2
[1] => BACK 2
)
)
修改结果
如果你想知道关于的stripslashes
,这是因为实际上保存在字符串
Edit
If you wonder about stripslashes
, it is because the string saved actually is
{\"CARD 1\":[\"FRONT I\",\"BACK I\"],\"CARD 2\":[\"FRONT 2\",\"BACK 2\"]}
的setcookie
quoutes逃脱他们之前添加 \\
。如果你无法摆脱那些, json_de code
将失败。
setcookie
adds \
before quoutes to escape them. If you not get rid of those, json_decode
will fail.
修改II
要添加新卡到cookie
To add a new card to the cookie
- 阵列负载如上
-
$ savedCardArray ['卡XX'] =阵列('XX FRONT,BACK XX');
- 将数组保存同上,但现在当然
$ savedCardArray
,而不是$ cardArray
。
- 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
.
这篇关于存储和检索在PHP中的cookie数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!