在 PHP cookie 中存储和检索数组 [英] Storing and retrieving an array in a PHP cookie

查看:27
本文介绍了在 PHP cookie 中存储和检索数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一些虚拟"索引卡中存储一些数据.每张卡片都有正面和背面,用户可以存放多张卡片.每一方都会有数据.

I ----------------- I I CARD 1 FRONT I -----------------II --------------- I I CARD 1 BACK I -----------------II ----------------- I I CARD 2 FRONT I -----------------II --------------- I I CARD 2 BACK I -----------------I

好吧,我的图表有点乱.但是你会收到消息.:)

从上图中想象一下.我想将每张卡片的数据(正面和背面)存储在 cookie 中,作为一个数组(也许),然后能够将每个值拉回并在适用的地方(在不同的页面上)插入.

同时请记住,用户可以制作任意数量的卡片.我不能使用 POST 或 GET 函数.数组位是有争议的,如果您能想到一种更简单的方法将这些数据存储在 cookie 中,请告诉我.请注意:不建议存储在数据库中,因为它对项目不方便.:)

解决方案

使用 json_encode/json_decode 来获取/设置 cookie 中的数组.

测试数组

$cardArray=array('CARD 1'=>array('FRONT I', 'BACK I'),'卡 2'=> 数组('前 2','后 2'));

转换并写入cookie

$json = json_encode($cardArray);setcookie('cards', $json);

保存的字符串看起来像这样

{"CARD 1":["FRONT I","BACK I"],"CARD 2":["FRONT 2","BACK 2"]}

取回饼干

$cookie = $_COOKIE['cards'];$cookie = stripslashes($cookie);$savedCardArray = json_decode($cookie, true);

显示恢复后的数组

echo '

';打印_r($savedCardArray);echo '</pre>';

输出

数组([卡片 1] =>大批([0] =>前我[1] =>返回我)[卡片 2] =>大批([0] =>前 2[1] =>返回 2))

<小时>

编辑
如果你对stripslashes感到疑惑,那是因为实际保存的字符串是

{\"CARD 1\":[\"FRONT I\",\"BACK I\"],\"CARD 2\":[\"FRONT 2\",\"BACK 2\"]}

setcookie 在引号前添加 \ 来转义它们.如果你不摆脱这些,json_decode 将会失败.

<小时>

编辑二

向 cookie 添加一张新卡片

  1. 如上加载数组
  2. $savedCardArray['CARD XX']=array('FRONT XX', 'BACK XX');
  3. 如上保存数组,但现在当然是$savedCardArray 而不是$cardArray.

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).

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. :)

解决方案

Use json_encode / json_decode to get / set arrays in cookies.

Test array

$cardArray=array(
    'CARD 1'=>array('FRONT I', 'BACK I'),
    'CARD 2'=>array('FRONT 2', 'BACK 2')
);

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"]}

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

{\"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.


Edit II

To add a new card to the cookie

  1. load the array as above
  2. $savedCardArray['CARD XX']=array('FRONT XX', 'BACK XX');
  3. save the array as above, but now of course $savedCardArray and not $cardArray.

这篇关于在 PHP cookie 中存储和检索数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆