As defined by dictionary.com a cookie is described as:
QUOTE
Computer Science: A collection of information, usually including a username and the current date and time, stored on the local computer of a person using the World Wide Web, used chiefly by websites to identify users who have previously registered or visited the site.
Cookies basically store information on a user, you must have seen those ‘Remember Me’ buttons on login forms, these forms set cookies on your hard drive with your username and password inside them (or any other information specified).
When a user visits your site you can store certain information about them, such as browser, IP address, operation system etc, but the problem with this is when the user comes back they may not have the same IP address (which is the most common way to recognise a user). By setting a cookie on a user’s computer it will remember that particular user when they return and read the information from it, even if the user has a different IP address (which is most likely).
What can you do with a cookie?
You can set a cookie on a user’s computer so that you can remember them when they return in the future … only problem being a lot of people delete their temporary internet files and cookies frequently because of clutter, this is why most webmasters set a time limit on their cookie to delete itself after an hour.
The PHP Manual has all the information you need on manipulating cookies in the set cookie function page but it is quite a read, I hope to simplify using cookies as much as possible.
Setting A Basic Cookie
To set a cookie on a user’s computer you would use the setcookie() function, using PHP it would look like this:
CODE
<?php
setcookie();
?>
setcookie();
?>
Now to create a very basic cookie:
CODE
setcookie("Joe2Torials",$username);
This example would store the user's name in a cookie called 'Joe2Torials'. By setting cookies like this, you don't set any specific options, so by default the cookie will be available to the domain in which it was set (your site) and it would be deleted when the user closed the browser.
Reading from a Cookie
PHP makes it very easy to read from cookies by using global values similar to $_POST and $_GET.
CODE
$_COOKIE['Joe2Torials'];
Using this variable it would grab the cookie with the name Joe2Torials, but it doesn’t do anything with it, how about displaying the already stored name on the page, this would be done like so:
CODE
<?php
echo "Welcome back ".$_COOKIE['Joe2Torials']."";
?>
echo "Welcome back ".$_COOKIE['Joe2Torials']."";
?>
This would grab the cookie with the name ‘Joe2Torials’ and display it on the page, since we have only stored the $username as above that’s all that would be displayed. Of course, what if the user didn’t have a cookie set in the first place? For this we should use the isset function which determines whether a variable is set.
CODE
<?php
if(isset($_COOKIE['Joe2Torials'])
{
echo "Welcome back ".$_COOKIE['Joe2Torials']."";
}
else
{
setcookie("Joe2Torials",$username);
}
?>
if(isset($_COOKIE['Joe2Torials'])
{
echo "Welcome back ".$_COOKIE['Joe2Torials']."";
}
else
{
setcookie("Joe2Torials",$username);
}
?>
As you can see, if the cookie exists it displays the username on the page, if the cookie doesn’t exist then it sets one. If you would like to learn more then move on to part 2 of my cookies tutorial with: Reading & Writing Multiple Cookies.
By Joseph Skidmore