<?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'];
?>
setcookie()
` function. We set the cookie to expire after 30 days and be available on the entire website ("/
").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.