Tutorials

Learn web design and programming with our free video and text tutorials.

PHP Cookies Tutorial

Cookies are small files that the web server embeds on the user's computer and can be used identify that user. Every time that the same computer requests a page with a web browser, it will also send the cookie. PHP allows you to create and retrieve cookie values.

How To Create a Cookie

To create a cookie, the setcookie function is used. The setcookie function accepts 1 required argument and 4 optional arguments.

setcookie($cookiename, $cookievalue, $expiration, $path, $domain, $secure)

The first argument is a string that represents the name of the cookie value that you will set, for example, "username".

The second argument represents the value that you want to store in the cookie, for example, "Joe".

By default, if the expiration date is not set, the cookie will be treated as a session cookie and be deleted when a user closes the web browser.

The path argument represents where on the web server the cookie will be available. For example, if the path argument is set to "/", the cookie will be made available through out the entire site. If the cookie is set to a sub-directory, it will be available only in that sub-directory and all of its sub-directories. If no path is given, the cookie will be created under the current directory.

The domain argument is where the cookie will be available, using domain settings instead of path settings.

For example, if you set the domain to ".domain.com", the cookie will be made available within the domain and all of its sub-domains, for example "directories.domain.com".

If the cookie is set to "www.domain.com" the cookie will be made available under all www sub-domains, for example "domain.com/directories".

The secure argument is true if the cookie is being set over a secure "https" server, otherwise it is false. The default value for the secure argument is false.

The code below creates a cookie:

A cookie must be set before any (X)HTML code.

<?php
setcookie("username", "Joe", time()+3600);
?>
<html>
<body>

</body>
</html>

The cookie name is "username" and the value of the cookie is "Joe". The cookie is set to expire after 1 hour, "+3600". The function time() retrieves the current timestamp and 3600 seconds (1 hour) is appended to the current time which will make the cookie expire after 1 hour.

The cookie below will be set to a month.

<?php
setcookie("username", "Joe", time()+60*60*24*30);
?>
<html>
<body>

</body>
</html>

The cookie below will be set to a year.

<?php
setcookie("username", "Joe", time()+(60*60*24*365));
?>
<html>
<body>

</body>
</html>

The PHP $_COOKIE variable is used to retrieve a cookie value.

<html>
<body>

<?php
echo $_COOKIE["username"];
?>

</body>
</html>

The isset() function is used to find out if a cookie has been set.

<html>
<body>

<?php
if (isset($_COOKIE["username"]))
{
echo "Welcome back" . $_COOKIE["username"] . "<br />";
}
else
{
echo "Welcome";
}
?>

</body>
</html>