PHP Cookies: What is, How to Create, Modify, Retrieve & Delete?

Using PHP you can Create, Modify, Retrieve, and even delete cookies. Do you want to know what cookies you can use in your PHP code?

Cookies are small files that are embedded in the user’s computer from a website.

It is used to identify a user when the client request a page this will send a cookie too.

Using PHP we can easily create and retrieve values stored in cookies.

Cookies are essential for websites and give webmasters information about their visitors.

For example, depending on websites it is used for tracking user login information, shopping cart, browser information, and so on.

You can call cookies text files that contain user-specific information and are created upon connection by the server.

How to Create cookies with PHP

Using the function setcookie() we can set cookies.

Here is the syntax for this function:

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

Except for the first parameter “name”, all parameter in this function is optional.

So if we want to create cookies using this syntax, here is how it’s done:

# Defining cookie variable:

$cookie_name = "user";
$cookie_value = "Rahul Bodana";
$cookie_duration = time() + (86400 * 30);

# Setting up the cookie:

setcookie($cookie_name, $cookie_value, $cookie_duration);

Here is the explanation for the code above:

  • In cookie_duration variable we set the duration from current time to next 30 days. It means it’ll expire after 30 days once set.
  • Please note down that The setcookie() function must appear BEFORE the tag, in HTML file.

We learned how to set cookies using the PHP function setcookie(), the next step is obviously to learn how to retrieve that cookie.

Here we would not only retrieve cookies using the cookie name but also check that the cookies are set properly.

isset() the function is used to check whether the cookies are set or not.

Here is the complete example:

if(!isset($_COOKIE[$cookie_name])){
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
  echo "Value is: " . $_COOKIE[$cookie_name];
}

Modifying the cookie is not different from setting up the cookie.

To modify a cookie we just set it again with the values that we want to replace.

Here is how it can be done:

# Defining cookie variable:

$cookie_name = "user";
$cookie_value = "Sarvin Batra";
$cookie_duration = time() + (86400 * 30);

# Setting up the cookie:

setcookie($cookie_name, $cookie_value, $cookie_duration);

Deleting cookies is a little bit tricky but not too complex.

The trick is simple if you want to delete a cookie all you need to do is set the time in the past.

For example, if you set the cookie expiration time even 1 minute ago from the current time, it would be deleted.

Here is how can delete it:

setcookie("user", "John doe", time() - 3600);

In one minute there are 60 seconds so in one hour it is 3600 seconds (60 * 60).

Using the (-) symbol with 3600 and time() function, we set the expiration time 1 hour in the past.

This code would delete that particular cookie.

How to confirm if Cookies are enabled?

Well, we learned What are cookies, how to create cookies, how to retrieve cookies, how to modify them, and even how one can delete cookies.

But what about making sure that cookies are enabled and working correctly.

Well, we don’t need a specific function to achieve that task since we could achieve that result by simple conditionals:

if(count($_COOKIE) > 0) {
  echo "Cookies are enabled.";
} else {
  echo "Cookies are disabled.";
}

If cookies are set it would echo that cookies are enabled otherwise disabled message.

Rahul Bodana passionately shares knowledge through tutorials on programming, investment, trading, gaming, and writing, aiming to empower others with valuable insights and how-to guides.