php PHP如何字符串化数组并存储在cookie中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4672130/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:56:18  来源:igfitidea点击:

PHP how to stringify array and store in cookie

php

提问by angry kiwi

I got an array like this

我有一个这样的数组

$value = {array('id'=>$id, 'email'=>$email, 'token'=>$token)}

$value = {array('id'=>$id, 'email'=>$email, 'token'=>$token)}

I want to stringify the array then encode then store it in cookie "login". How do you do that ? Also please tell me how to decode and read the stored value.

我想对数组进行字符串化,然后编码,然后将其存储在 cookie“登录”中。你是怎样做的 ?另外请告诉我如何解码和读取存储的值。

Edit:

编辑:

I've been trying serialize/unserialize, but it didn't work as expected. for example,

我一直在尝试序列化/反序列化,但没有按预期工作。例如,

$value = serialize(array('id'=>33, 'email'=>'[email protected]', 'token'=>'e9aa0966773d68e0fbf9cb21fc2877b4'));

echo $value; //a:3:{s:2:"id";i:33;s:5:"email";s:20:"[email protected]";s:5:"token";s:32:"e9aa0966773d68e0fbf9cb21fc2877b4";}

But when the value go to cookie, it looks like this

但是当值转到 cookie 时,它​​看起来像这样

a%3A3%3A%7Bs%3A2%3A%22id%22%3Bs%3A1%3A%226%22%3Bs%3A5%3A%22email%22%3Bs%3A20%3A%22craigcosmo%40gmail.com%22%3Bs%3A5%3A%22token%22%3Bs%3A32%3A%22e9aa0966773d68e0fbf9cb21fc2877b4%22%3B%7D

回答by Brad Christie

json_encode/json_decode

json_encode/ json_decode

$_COOKIE['login'] = json_encode($array);
$array = json_decode($_COOKIE['login']);

Can also use serialize/unserialize:

也可以使用序列化/反序列化

$_COOKIE['login'] = serialize($array);
$array = unserialize($_COOKIE['login']);

Perhaps.

也许。



UPDATE

更新

With this code:

使用此代码:

<html><body><pre><?php
  $array = Array(
    'id'  => 1234,
    'email' => '[email protected]',
    'token' => base64_encode('abcDEF1234')
  );

  echo "Var Dump (initial):\r\n";
  var_dump($array);

  $serialized = serialize($array);
  echo "Serialized:\r\n".$serialized."\r\n";

  $unserialized = unserialize($serialized);
  echo "Unserialized:\r\n".$unserailized."\r\n";
  var_dump($unserialized);
?></pre></body></html>

You would generate the following:

您将生成以下内容:

Var Dump (initial):
array(3) {
  ["id"]=>
  int(1234)
  ["email"]=>
  string(19) "[email protected]"
  ["token"]=>
  string(16) "YWJjREVGMTIzNA=="
}
Serialized:
a:3:{s:2:"id";i:1234;s:5:"email";s:19:"[email protected]";s:5:"token";s:16:"YWJjREVGMTIzNA==";}
Unserialized:

array(3) {
  ["id"]=>
  int(1234)
  ["email"]=>
  string(19) "[email protected]"
  ["token"]=>
  string(16) "YWJjREVGMTIzNA=="
}


EDIT2

编辑2

You're seeing the encoded value based on how the HTTP protocol transfers cookies. There are two headers in a cookie transfer: Set-Cookie& Cookie. One is server->client, other other is client->server, respectfully.

您将看到基于 HTTP 协议传输 cookie 的方式的编码值。cookie 传输中有两个标头:Set-Cookie& Cookie。一种是服务器->客户端,另一种是客户端->服务器,恭敬。

When PHP sets the cookie (using setcookie e.g.) PHP is really just short-handing the following:

当 PHP 设置 cookie(例如使用 setcookie)时,PHP 实际上只是简写了以下内容:

setcookie('login',$serialized);

which, in PHP translates to:

其中,在 PHP 中转换为:

header('Set-Cookie: login='.urlencode($serialized).'; '
      .'expires=Wed, 12-Jan-2011 13:15:00 GMT; '
      .'path=/; domain=.mydomain.com');

If you had characters like :or a SPACE, the browser wouldn't know where the cookie's properties began and ended.

如果您有像:或 SPACE这样的字符,浏览器将不知道 cookie 属性的开始和结束位置。

回答by Jonathan Kuhn

there is a serialize/unserializefunction to convert an array to a string and back.

有一个序列化/反序列化函数可以将数组转换为字符串并返回。

Edit: When you store a string to cookie (setcookie), php needs to do a url encode on the string. This prevents any characters in the string saved to cookie interfering with any other headers. When the page is loaded next, php gets the cookie and automatically does a url decode on the cookie value to return it to it's previous value. As far as what is stored in the cookie, this shouldn't matter within php because php will do the url encode/decode automatically. Now if you are getting the cookie in another language such as javascript, then yes, you will get the raw string back. In this case you can use something like decodeURIin JS to get the original value back.

编辑:当您将字符串存储到 cookie ( setcookie) 时,php 需要对字符串进行 url 编码。这可以防止保存到 cookie 的字符串中的任何字符干扰任何其他标题。当页面下次加载时,php 获取 cookie 并自动对 cookie 值进行 url 解码以将其返回到它的先前值。至于存储在 cookie 中的内容,这在 php 中应该无关紧要,因为 php 会自动进行 url 编码/解码。现在,如果您以另一种语言(例如 javascript)获取 cookie,那么是的,您将返回原始字符串。在这种情况下,您可以使用JS 中的decodeURI 之类的东西来获取原始值。