Cookies
The following example creates a cookie named "user" with the value "John". The cookie will expire after 30 days, which is written as 86,400 * 30, in which 86,400 seconds = one day. The '/' means that the cookie is available throughout the entire website.
We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:
<?php
$value = "John";
setcookie("user", $value, time() + (86400 * 30), '/');
if(isset($_COOKIE['user'])) {
echo "Value is: ". $_COOKIE['user'];
}
//Outputs "Value is: John"
?>
$value = "John";
setcookie("user", $value, time() + (86400 * 30), '/');
if(isset($_COOKIE['user'])) {
echo "Value is: ". $_COOKIE['user'];
}
//Outputs "Value is: John"
?>
The setcookie() function must appear BEFORE the <html> tag.
The value of the cookie is automatically encoded when the cookie is sent, and is automatically decoded when it's received. Nevertheless, NEVER store sensitive information in cookies.
The value of the cookie is automatically encoded when the cookie is sent, and is automatically decoded when it's received. Nevertheless, NEVER store sensitive information in cookies.
No comments:
Post a Comment