PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Introduction To Cookies
Your Ad Here

Introduction To Cookies


Introduction To CookiesAs defined by dictionary.com a cookie is described as:

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:

<?php
setcookie();
?>

Now to create a very basic cookie:

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.

$_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:

<?php
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.

<?php
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.



Author's URL: Joseph Skidmore
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Introduction To Cookies"

Only registered users can write comment

No comments yet...