Cookies
To create and modify a cookie, use the PHP function setcookie():
setcookie('uid', 'John');
Then, you can access the value "John" of the variable $uid
in every further page on your site viewed by this browser. This type of
cookie is called "session cookie", because it lasts for the
length of a user's session.
If you want to keep the cookie after the person exits his or her browser,
you must pass setcookie() through a third parameter, the date you want
the cookie to expire:
<?php
$uName = 'John';
$exitDate = mktime(0,0,0,1,1,2002);
setcookie('uid', $uName, $exitDate);
?>
So the cookie will end with January 1, 2002.
If you want to update a cookie to store a newer value, you can simply
overwrite its value:
<?php
$uName = 'Mike';
$exitDate = mktime(0,0,0,1,1,2002);
setcookie('uid', $uName, $exitDate);
?>
setcookie() can take up to six arguments. The below cookie has five arguments:
<?php
setcookie('uid', 'John', $exitDate, '~/home', '.abc.com');
?>
It is very simple to delete a cookie: simply left the value of varible
as null and just keep the name:
<?php setcookie('uid'); ?>
Note: any cookie must be set before you print any other contents:
<?php
setcookie('uid', 'John');
echo "Hello world";
?>
|