douxunwei8259 2011-10-07 19:15
浏览 41
已采纳

将键添加到现有(或非)数组

I got this code:

if( empty ($cache[$id]) ) {
    $arr[$id] = @TIMENOW;
    setcookie('id', cArr($arr, 'set'), -1, @PATH);
} else {
    $cache[$id] = @TIMENOW;
    setcookie('id', cArr($cache, 'set'), -1, @PATH);
}

And it is adding only one key, to the cookie, if I'll go to the another thread , it'll reset the array, and won't add more keys. I mean, if the user goes to the thread with id 1 then if( empty ($cache[1]) ) is adding 1, instead it'll update existing value, AND if user will go now to the thread with ID 5, it will do the same, and if( empty ($cache[5]) ) is empty , then it'll add the key with ID 5 to the array so I'll have both keys now: 1 and 5.

Hope you got it. If you don't , feel free to ask for whatever you wan't, I'll reply for all of your quesitons.

  • 写回答

1条回答 默认 最新

  • duanaoyuan7202 2011-10-07 19:51
    关注

    It would be helpful to know what you're doing in cArr(). But without it, this will add to your cookie for each new thread a user visits.

    //get previous values
    $id = $_GET['thread_id'];
    $cache = array_key_exists('id', $_COOKIE) ? unserialize($_COOKIE['id']) : array();
    
    //add to $cache
    $cache[$id] = TIMENOW;
    setcookie('id', serialize(cArr($cache, 'set')), -1, PATH);
    

    WARNING: But keep in mind, that with just setting a cookie, your webserver can be exploited. So better not use searialize and unserialize to store simple static values inside your cookie.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog