How to Create a Cookie with PHP

PHP

One of the methods for creating and saving HTTP cookies on the user's device is to do it with PHP, that is, if your website or application is built with PHP. In this tutorial, I will briefly demonstrate how to create a cookie using PHP.

A cookie is often times a harmless little file with a piece of information that is stored on your device's (computer, tablet or mobile phone) web browser when you visit a website that places cookies. Some websites may have no cookies at all, some may have a single one and some others may have multiple cookies depending on the nature of the site and the level of user interactions.

Cookies can store any information that you want but they are mostly used for storing user-specific information such as a session ID, user preference, actions on a form or anything similar to help identify the user and keep track of the important actions taken on the site. The information stored in the cookie is then checked at the next visit of the same user, and the website behaves according to that information.

Simple Example: When you log into your account on a website (e.g. Facebook or YouTube), a cookie that identifies your session is stored on your browser, so that the next time you visit the site even after closing and reopening your browser, you won't have to log in again which would have been quite cumbersome if you were to enter your login details whenever you visited the site.

Before I start, I should mention an important point about the use of cookies. If your website is accessible from any EU (European Union) country, your website should display a cookie notice and ask the consent of the EU users for placing cookies on their devices.

Set a Cookie with PHP

In PHP, a cookie is sent to the browser from the server using the setcookie() function. Since the cookie is sent with the other HTTP headers, it must be sent (created) before any output is displayed on the page. In other words, you should set your cookie using the setcookie() function at the top of your PHP file, before any other HTML content, including <html> tag or empty space.

The following is a basic example of how a cookie is set via PHP:

<?php
$cookie_name = "example";
$cookie_value = "example-1";
$cookie_path = "/";
$expiry_time = time() + 30 * 24 * 60 * 60;

setcookie($cookie_name, $cookie_value, $expiry_time, $cookie_path);
?>

The first argument of the setcookie() function is the name of the cookie (example), the second argument is the value of the cookie (example-1), the third argument is the expiry time of the cookie in seconds from the time of access to the page (30 days) and the fourth argument is the path of the cookie (/), which corresponds to the whole domain in this case.

This is the basic way of setting a cookie, you can check the definition of the setcookie function and detailed instructions on the PHP manual here.

Update a Cookie with PHP

After the cookie is set, you can update its value using the same function like the following:

<?php
setcookie("example", "example-1", time() + 60 * 60, "/");

// other actions...

setcookie("example", "example-2", time() + 60 * 60, "/");
?>

The above code will update the value of the example cookie from example-1 to example-2. Note that, you don't need to set and update the cookie on the same file, the above was just an example for the sake of simplicity.

Read a Cookie with PHP

The main purpose of setting a cooking is to read it, so that your script can behave accordingly. To read the value of the cookie, we use the global variable $_COOKIE[$cookie_name] like the following:

<?php
setcookie("example", "example-1", time() + 60 * 60, "/");
$example = $_COOKIE["example"];
?>

Check If a Cookie Exists

Before reading the cookie value, you should check the existence of the cookie like the following:

<?php
setcookie("example", "example-1", time() + 60 * 60, "/");

if (isset($_COOKIE["example"])) {
  echo "Cookie exists!";
}
?>

Finally, I want to mention some points about how cookies work which I believe may help you in designing your PHP application.

- Cookies will work only if the user has set the browser to accept cookies, otherwise they won't work.
- Cookies will be stored till their expiration time or till the user clears the browser cache or deletes a single cookie.
- When a user visits your site and a cookie is placed in browser A, it won't be available in browser B.

You can now continue with the next tutorial about how to delete a cookie with PHP.

f t g+ in