简体   繁体   中英

Update a cookie value without changing its expiry date?

Update a cookie value without changing its expiry date?

$c = $_COOKIE["count"];
$c++;
if (isset($_COOKIE["count"])) {
    setcookie("count", $c);
}
else
{
    setcookie("count", $c, time() + 86400, '/');
}

The only way you can update a cookie's value without updating its expiry date is by adding the expiry date itself into the value; that's because a browser only sends you the names and values of the cookies.

if (isset($_COOKIE['count'])) {
    list($exp, $val) = explode('|', $_COOKIE['count'], 2);
    ++$val;
} else {
    $exp = time() + 86400;
    $val = 1;
}
setcookie('count', "$exp|$val", $exp, '/');

你不能 - PHP不知道cookie什么时候到期也不知道javascript(除非你将到期时间复制 cookie内容中)。

You can save expiry date in other cookie. When you update your cookie, just read expiry date from it and use it in your update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM