Update a cookie value without changing its expiry date?

php, setcookie

Solution

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

Problem

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, '/'); } ```

Original source