Google News
logo
PHP Program to Modify a cookie value
In the following example of PHP program to modify a cookie value :
Program :
<?php
// set the cookie with initial value
$value = 'initial value';
setcookie('myCookie', $value, time() + (86400 * 30), "/"); // 86400 = 1 day

// check if cookie is set and modify its value
if(isset($_COOKIE['myCookie'])) {
    $value = $_COOKIE['myCookie'];
    $value = 'modified value';
    setcookie('myCookie', $value, time() + (86400 * 30), "/");
}

// output the cookie value
echo 'Cookie value: ' . $_COOKIE['myCookie'];
?>
In the above example, we first set a cookie with an initial value of "initial value" using the `setcookie()` function. We set the cookie to expire after 30 days and be available on the entire website ("/").

Next, we check if the cookie is set using the `isset()` function. If it is set, we retrieve its value and modify it to "modified value". We then set the cookie again with the new value using the `setcookie()` function.

Finally, we output the current value of the cookie using the `echo` statement.