PHP Cookies Explained

Last Updated: 02 Jan, 2024
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.

  • Cookies are created at server end 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.
  • Cookies are allowed to be read from the domain that it has been issued from.
  • Cookies are the part of the HTTP header, that's why setcookie() must be called before any output is sent to the browser.
  • Cookies are normally set in an HTTP header but JavaScript is also able set a cookie directly on a browser.
  • PHP transparently supports HTTP cookies.
There are three steps by following which you can identify returning users
  1. Server creates and sends a set of cookies to the browser.
  2. Browser is responsible to store this information on local machine of the user 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 recognise the returning users.

Example of real world usage 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.
  • You can store basic website specific information so that you can know that this is not the first visit of user.
  • With the help of Cookies, you can track and 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 encoded 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. Cookie's value is usually 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.
Example:
<?php
// 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:

<?php
// 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:

<?php
// 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:

<?php
// 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:

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

You can verify this as follows:

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

Recommended Posts

IMAGE

PHP OOP Abstract Classes Explained

A PHP class that has minimum one abstract method is called as abstract class and cannot be instantiated by itself.

IMAGE

PHP OOP Traits Explained

Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.

PHP Sessions Explained

PHP Sessions Explained

In this post you will be gaining valuable insights about the PHP sessions and it's working functionalities.