PHP Cookies

Last Updated: 09 Sep, 2022
What is a PHP Cookie?

A cookie is a small text file with the maximum size of 4KB. Cookie is a medium through which server stores data in the remote browser for the purpose of tracking and identifying return users. So, whenever same computer requests a page with a browser, it will send the cookie too.

  • Cookie is created at server side and saved to client browser.
  • Each time when client sends request to the server, all the data in the cookie is automatically sent to the server within the request.
  • A cookie can only be read from the domain that it has been issued from.
  • Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser.
  • Cookies are usually set in an HTTP header but JavaScript can also set a cookie directly on a browser.
  • PHP transparently supports HTTP cookies.
There are three steps involved in identifying returning users
  1. Server creates and sends a set of cookies to the browser.
  2. Browser stores this information on local machine for future use.
  3. Next time, when browser sends any request to same server then it sends those cookies information to the server and server uses that information to identify the user.
Why Cookie is important?

The web works based on the HTTP protocol. The HTTP protocol is stateless. When the web browser requests a page from a web server, the web server responds with the page content. Later, the same web browser requests the same page again, the web server has no information that the request is from the same web browser. Cookies solve this stateless challenge. By using Cookie mechanism, server can easily recognize the returning users.

Example of realworld use of Cookies:
  • To store user information like when user visited, what pages were visited on the website etc, so that next time the user visits your website you can provide a better user experience.
  • To store basic website specific information to know this is not the first visit of user.
  • You can use cookies to store number of visits or view counter.
Some important points to be noted regarding cookie:
  • The setcookie() function must appear BEFORE the <html> tag.
  • The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received.
  • To prevent URL encoding, you should use setrawcookie() instead.
  • If the cookie expiration time is set to 0 or omitted, it will expire at the end of the session.
How to set Cookie in PHP?

You can set cookie in PHP by using setcookie() function. Below is the syntax:

setcookie(name, value, expire, path, domain, secure);
Parameters description:
  • name: Used to set name of the cookie.
  • value: Used to set value of the cookie. This value is stored on the clients computer.
  • expire: Used to set cookie expiration time.
  • path: Used to set the path on the server in which the cookie will be available on.
  • domain: Used to set domain for which cookie will be available.
  • secure: Used to indicate that the cookie should only be transmitted over a HTTPS connection from the client.
// cookie expires in 1 minute
setcookie("Name", "W3TechPoint", time() + 60);

// cookie expires in 1 hour
setcookie("Name", "W3TechPoint", time() + 60 * 60);

// cookie expires in 24 hour
setcookie("Name", "W3TechPoint", time() + 24 * 60 * 60);
How to verify whether cookie is set or not?

Before you access any cookie, you must check whether it is set or not. You can verify is as follows:

// cookie expires in 1 minute
setcookie("MyName", "W3TechPoint", time() + 60);

if (isset($_COOKIE["MyName"])) {
    echo "My name is: " . $_COOKIE["MyName"];
} else {
    echo "Cookie is not available!";
How to access cookie in PHP?

You can access cookie value by using $_COOKIE superglobal variable. $_COOKIE is an associative array that holds all the cookies values. All the cookies are stored inside this array as list and cookie name is used as key. So, to access any particular cookie you can pass it's key to $_COOKIE array as follows:

// cookie expires in 1 hour
setcookie("MyName", "W3TechPoint", time() + 60 * 60);

echo "My name is: " . $_COOKIE["MyName"];
How to modify cookie value in PHP?

To modify a cookie, you just need to set (again) the cookie using the setcookie() function as follows:

// cookie expires in 1 hour
setcookie("MyName", "New Name", time() + 60 * 60);
How to delete cookie in PHP?

You can use setcookie() function to delete a cookie. To delete a cookie, use the setcookie() function with an expiration date in the past as follows:

// set the expiration time to one hour ago
setcookie("MyName", "W3TechPoint", time() - 3600);
How to check if cookies are enabled or not?

You can verify this as follows:

if(count($_COOKIE) > 0) {
    echo "Cookies are enabled.";
} else {
    echo "Cookies are disabled.";
Thank You, Please Share.