Setting a cookie
We will use the setcookie() function to do this. The parameters used in this example are:
"name " This will be the name of the cookie. "value" The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename'] expire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.
setcookie.php
code:
<?php
setcookie('username', 'maveric', time()+10);
?>
viewcookie.php
To view anytime the cookie status we need this piece of code:
code:
<?php
echo $_COOKIE['username'];
Removing a cookie
In this part we can use the previous files, with a small editing.
To remove a cookie happens simply by resetting the cookie. Instead of setting the cookie the value for time will be put to minus value for the previous plus value.
code:
<?php
setcookie('username', 'maveric', time()-10);
?>