向现有(或不存在)数组添加键

发布于 2024-12-08 12:10:53 字数 542 浏览 2 评论 0原文

我得到了这段代码:

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

它只向 cookie 添加一个键,如果我转到另一个线程,它会重置数组,并且不会添加更多键。我的意思是,如果用户转到 id 1 的线程,则 if(empty ($cache[1]) ) 会添加 1,而是会更新现有值,并且如果用户现在转到 ID 为 5 的线程,它将执行相同的操作,并且 if(empty ($cache[5]) ) 为空,然后它将添加密钥数组的 ID 为 5,这样我就拥有两个密钥现在:1 和 5。

希望你明白了。如果您不这样做,请随时提出您想要的任何问题,我会回答您的所有问题。

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.

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

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

发布评论

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

评论(1

梦归所梦 2024-12-15 12:10:53

了解您在 cArr() 中所做的事情会很有帮助。但如果没有它,这将为用户访问的每个新线程添加到您的 cookie 中。

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

警告:但请记住,只要设置 cookie,您的网络服务器就可能被利用。因此,最好不要使用 serializeunserialize 在 cookie 中存储简单的静态值。

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.

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