如何在 php 中添加/编辑 cookie?

发布于 2024-09-13 11:06:19 字数 810 浏览 2 评论 0原文

我正在使用以下函数在 PHP 中的 cookie 中设置值数组,但我还需要一个“添加”和“编辑”函数 - 关于如何做到这一点有什么建议吗?

function build_cookie($var_array) {
  if (is_array($var_array)) {
    foreach ($var_array as $index => $data) {
      $out.= ($data!="") ? $index."=".$data."|" : "";
    }
  }
  return rtrim($out,"|");
}

function break_cookie ($cookie_string) {
  $array=explode("|",$cookie_string);
  foreach ($array as $i=>$stuff) {
    $stuff=explode("=",$stuff);
    $array[$stuff[0]]=$stuff[1];
    unset($array[$i]);
  }
  return $array;
}

用法:

setcookie("mycookies", build_cookie($cookies_array), time()+60*60*24*30);

$cookies_array2 = break_cookie(urldecode($_COOKIE['mycookies']));

    foreach ($cookies_array2 as $k => $v) {
        echo "$k : $v <br />\n";
    }

I'm using the following functions to set an array of values in a cookie in PHP, but I also need an "add" and "edit" function - any suggestions on how I can do that?

function build_cookie($var_array) {
  if (is_array($var_array)) {
    foreach ($var_array as $index => $data) {
      $out.= ($data!="") ? $index."=".$data."|" : "";
    }
  }
  return rtrim($out,"|");
}

function break_cookie ($cookie_string) {
  $array=explode("|",$cookie_string);
  foreach ($array as $i=>$stuff) {
    $stuff=explode("=",$stuff);
    $array[$stuff[0]]=$stuff[1];
    unset($array[$i]);
  }
  return $array;
}

Usage:

setcookie("mycookies", build_cookie($cookies_array), time()+60*60*24*30);

$cookies_array2 = break_cookie(urldecode($_COOKIE['mycookies']));

    foreach ($cookies_array2 as $k => $v) {
        echo "$k : $v <br />\n";
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲娇萝莉攻 2024-09-20 11:06:19

您应该考虑使用的一件事是 serialize反序列化 对您的 Cookie 数据进行编码。不过要小心,根据我的经验,你必须在 cookie 上使用 stripslashes反序列化之前的值。这样您就可以反序列化数据、更改值、重新序列化 cookie 并再次发送。如果您想存储更复杂的数据类型,序列化将使您将来变得更容易。

例如:

setcookie("mycookies",serialize($cookies_array),time()+60*60*24*30);

// This won't work until the next page reload, because $_COOKIE['mycookies']
// Will not be set until the headers are sent    
$cookie_data = unserialize(stripslashes($_COOKIE['mycookies']));
$cookie_data['foo'] = 'bar';
setcookie("mycookies",serialize($cookies_array),time()+60*60*24*30);

One thing that you should consider using is serialize and unserialize to encode your cookie data. Just be careful though, from my experience you have to use stripslashes on the cookie value before you unserialize it. This way you can unserialize the data, change the values, reserialize the cookie and send it again. Serialize will make it easier in the future if you want to store more complex data types.

for example:

setcookie("mycookies",serialize($cookies_array),time()+60*60*24*30);

// This won't work until the next page reload, because $_COOKIE['mycookies']
// Will not be set until the headers are sent    
$cookie_data = unserialize(stripslashes($_COOKIE['mycookies']));
$cookie_data['foo'] = 'bar';
setcookie("mycookies",serialize($cookies_array),time()+60*60*24*30);
墨落画卷 2024-09-20 11:06:19

我会在 cookie 中仅存储一个 id,并使用平面文件(ini、序列化或纯文本)或数据库来存储值。问题是 - cookie 的空间受到严重限制,您应该添加尽可能少的内容。我的最新项目之一是,我必须存储大量信息,并且由于我可以访问 SSD 驱动器,因此我将序列化的数组和对象放入压缩文件中,并在 cookie 中保存了 id,然后是各个部分的一些哈希值。数据能够进行一些快速验证。

而且,从安全角度来看,仅拥有一个 id(以及本地数据的哈希值,这样就无法轻易更改 id 或该 id 的某种其他形式的验证)比将数据放入 cookie 更安全。

您有什么特殊原因将数据保存为 cookie 吗?

I would store just an id in the cookie and use a flat file (ini, serialized or plain text) or database to store the values. The thing is - cookie are severely space-limited and you should add as little as possible. One one of my latest projects i had to store alot of information and, since i had access to ssd drives, i put the arrays and objects serialized in zipped files and in the cookies i saved the id, and then some hashes of varios parts of the data to be able to do some quick validation.

And, from a security point of view, having just an id (and a hash of the local data so one can't easily change the id or some other form of verification of that id) is more secure than putting the data in the cookie.

Do you have any special reason to save data as cookies?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文