如何在PHP中添加/编辑cookie? [英] How to add / edit a cookie in php?

查看:222
本文介绍了如何在PHP中添加/编辑cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下函数在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 /> \\\

}


解决方案

使用是序列化 unserialize 来编码您的Cookie数据。但请注意,根据我的经验,您必须在Cookie上使用 stripslashes 值,然后再取消序列化。这样,您可以取消序列化数据,更改值,保留Cookie并再次发送。



例如:



<$

如果您想存储更复杂的数据类型, p $ p> setcookie(mycookies,serialize($ cookies_array),time()+ 60 * 60 * 24 * 30);

//这将不工作,直到下一页重新加载,因为$ _COOKIE ['mycookies']
//将不设置,直到标题发送
$ cookie_data = unserialize(strips_ashes($ _ COOKIE ['mycookies']));
$ cookie_data ['foo'] ='bar';
setcookie(mycookies,serialize($ cookies_array),time()+ 60 * 60 * 24 * 30);


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

解决方案

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

这篇关于如何在PHP中添加/编辑cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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