What Are Cookies In PHP And How To Use Them

Posted by TotalDC

In today’s article in this PHP tutorial series we will talk about PHP cookies. We will discuss what are they, how to create them, and why you would want to do that.

So What Is A Cookie In PHP?

So what is a cookie you may be asking? A cookie is a small text file that lets you store a small amount of data (almost 4KB) on the user’s computer. They are used to keep track of information such as username that the site can retrieve to personalize the page when users visit the website etc.

How To Setup A Cookie In PHP

The setcookie() function is used to set up a cookie in PHP. You have to call the setcookie() function before any output is generated by your script because then the cookie will not be set. The basic syntax of this function looks like this:

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

There are a lot of parameters in this function. Here are the meanings of them:

  • name – The name of the cookie.
  • value – The value of the cookie. Do not store sensitive information since this value is stored on the user’s computer.
  • expires – The expiry date in UNIX timestamp format. After this time cookie will become inaccessible. The default value is 0.
  • path – Specify the path on the server for which the cookie will be available. If set to /, the cookie will be available within the entire domain.
  • domain – Specify the domain for which the cookie is available to e.g www.yourwebsite.com.
  • secure – This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection exists.

Now let’s look at the example of setcookie() function used to create a cookie named username and assign the value Web Stuff to it. And then specifies that the cookie will expire after 30 days.


setcookie("username", "Web Stuff", time()+30*24*60*60);

How to Access Values From Cookie In PHP

Ok, so you created your first cookie, assigned some value to it and now you need to access that value that you stored. What to do? The PHP $_COOKIE superglobal variable is used to retrieve a cookie value. It is an associative array that contains a list of all the values inside the cookie. The individual cookie value can be accessed by using standard array syntax. For example to display the username from the cookie from the previous example you could use the following code:


print $_COOKIE["username"];


Web Stuff

It’s a good practice to check whether a cookie is set or not before trying to access its values. To do this you would use isset() function. Here is an example:


    print "Hello " . $_COOKIE["username"];
} else{
    print "Welcome To Simply Web Stuff!";

How to Remove Cookies In PHP

If you want to delete a cookie you can do that by calling the same setcookie() function with the cookie name and any value and setting a negative expiration date.


setcookie("username", "", time()-3600);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.