Cookie can be created and deleted after particular time automatically or deletion can also be done manually.
setcookie()
function.
In the above syntax only one parameter name is required and the remaining are optional.
[php]
<!DOCTYPE html>
<?php
$cookie_name = "Student";
$cookie_value = "Mike";
setcookie($cookie_name, $cookie_value, time() + (5 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named ‘" . $cookie_name . "’ is not set!";
} else {
echo "Cookie ‘" . $cookie_name . "’ is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
<p><strong>Note:</strong> Please reload the page to see the cookie value.</p>
</body>
</html>
[/php]
Output:
<html>
tag.
The cookie value is URLencoded automatically while the cookie is being sent and gets decoded automatically when received (to avoid URLencoding, setrawcookie() has to be used).
[php]
<!DOCTYPE html>
<?php
setcookie("test_cookie", "test", time() + 3600, ‘/’);
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
[/php]
Output: