PHP Cookies

No, I’m not talking about chocolate chip cookies here. Cookies are used to store a bit of information on the users computer for the current visit, or for subsequent visits to your site.

Amongst other things, this information could include a session after a user has logged in to a password protected area of your site, or a cookie could be used to track affiliate referrals.

Creating A Cookie

To create a cookie, we use the setcookie() function.

A cookie is set using the following variables:

setcookie(name, value, expire, path, domain, security);

Name

Each cookie has a unique name based on the information it needs to store.

Value

The value can be set as a username once a member logs in to your site. For example, “John Robinson”.

Expire

This is the time when the cookie will expire. For example, if a cookie is set to expire after 1 month, they will not be recognized and will thus have to log back in again.

The expire time can be calculated by the following formula:

seconds * minutes * hours * days + current time

Path

The path value represents the path on your domain that can use the cookie. For example, if a cookie is to be used to recognize a user logged in to your site, the path could be “/members”.

Domain

The domain sets where on your site the cookie is available. If there is no value for the domain, the cookie will be made available at the location where it was set.

Security

Security is used to specify if the cookie will be sent over a secure connection (HTTPS). The number 1 represents that it be sent over HTTPS while the value 0 represents a standard connection (HTTP). It is important to note that just because you set the security value to 1, it does not mean that the cookie will be secure- it just specifies the connection that will be used to send the cookie to the visitor’s computer.

Setting A Cookie

To set a cookie, we could code something like this:


setcookie("username", "John Robinson", time()+7200);

In the above example, we set a cookie named “username” which stores the value “John Robinson” and is set to expire in two hours (7200 seconds).

By our expire time formula mentioned above:

seconds * minutes * hours * days + current time

We can set the cookie to expire in one month instead of two hours by assigning the expire time a variable:


$expire=time()+60*60*24*30;
setcookie("username", "John Robinson", $expire);

In the above example, the cookie expires one month in the future (60 seconds * 60 minutes * 24 hours * 30 days)

Retrieving A Cookie

The associative array $_COOKIE allows us to retrieve a cookie from a user’s computer which has not yet expired.

An example of retrieving a cookie from a user’s computer can look like:


if(isset($_COOKIE['username']))
$member = $_COOKIE['username'];
else echo "Welcome, Guest";

echo "Welcome, ". $member;

If the visitor is returning within a month (assuming we’re using the cookie example above), this bit of code would display:

Welcome, John Robinson

If the visitor is returning after a month (after the cookie has expired), this bit of code would display:

Welcome, Guest

Deleting A Cookie

You can set a time to delete the cookie.

An example of deleting a cookie after a determined amount of time can look like:


agosetcookie("username", "", time()-7200);

In the above example, the cookie named “username” is deleted after two hours (7200 seconds).