PHP Cookies

Cookie Basics :

A cookie (HTTP cookie) is a small piece of data sent from a website and stored in user's computer by the user's browser. They are designed to hold a modest amount of data specific to a particular client and website, and can be accessed either by the web server or the client computer. The users data like user name, email address, authentication details etc information are stored on the cookie.

When a web server has sent a web page to a browser, and the TCP connection is closed, then the serer forgets everything about the user. To solve this problem cookies are used, now when a browser requests a web page to a server, then the cookies belonging to that page or webiste will added to the request. This way the server gets the necessary information about users and provide them better user experience.

Cookies are stored on computers in key-value pairs. In HTTP communication , the Set-Cookie response header is used to set the cookies. For example
 Set-Cookie: name=marry, expires=Mon, May 28 2018 15:00:00 GMT
The above statement will set the cookies with the name and expires filed, where the name field stores the username and the expires field stores the expiry date of the cookie. Some of the cookie attributes are as follows :

Name=Value : Cookies are set and retrieve in the form of key-value pairs.

Expires : The expires attribute set the expiry date/time for the cookie. If this field is blank, then the cookie will expire when the visitor quits the browser.

Domain : The domain attribute specifies those hosts to which the cookie will be sent.

Path : The path attribute Indicates a URL path that must exist in the requested resource before sending the Cookie header.

secure : A secure attribute specifies that the cookie will only be sent to the server when a request is made using SSL and the HTTPS protocol.

HttpOnly : The cookie with HTTPOnly attribute are not accessible to javascript's document.cookie API.

Cookies in PHP :

In PHP the setcookie() function is used to set/create a cookie. The setcookie() function will take upto six argument. The syntax of setcookie() is :
 setcookie(name, value, expire, path, domain, secure, httponly);
The parameters of setcookie() function has following meaning :

name : name of the cookie.

value : value of the cookie.

expires : Sets the expiry date/time for the cookie.

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, for example www.example.com.

secure : Specifies that the cookie will only be sent to the server when a request is made using SSL and the HTTPS protocol.

HttpOnly : specify that the cookie is not accessible to javaScript's document.cookie API.

Where the name parameter is required and all other parameters are optional. Once cookie is created, we can access it by using $_COOKIE superglobal variable. The syntax for accessing cookies is :
 $value = $_COOKIE['cookie_name'];
The above code returns the cookie value and stored it into the variable $value. Now lets see an example :
<?php

  setcookie("user", "John wick", time()+(60*60*1), "/", "localhost");

  if(!isset($_COOKIE["user"])) {
    echo "cookie is not set.";
  } else {
    echo $_COOKIE['user'];
  }

?>
Output :

John wick

The above code set a cookie named user with the value John wick. The expiry date of cookie is set to one hour ( time()+(60*60*1)), path is set to / (root), domain is set to localhost. And to check that the cookie is created or not isset() function is used. If the cookie is created then the function will return true, otherwise false.

Modifying the Cookie :

To modify the cookie values, we just need to set the cookie value again with the setcookie() function for example:
<?php

  setcookie("user", "John wick", time()+(60*60*1), "/", "localhost");
  setcookie("age", 50);
  setcookie("email", "Johnwick@killer.net");

  // modify the cookies
  setcookie("user", "John snow", time()+(60*60*1), "/", "localhost");
  setcookie("age", 32);
  setcookie("email", "John@kingofnorth.net");

  echo $_COOKIE['user'] . "<br/>";
  echo $_COOKIE['age'] . "<br/>";
  echo $_COOKIE['email'] . "<br/>";

?>
Output :

John snow
32
John@kingofnorth.net

Deleting Cookie :

To delete a cookie in php, we just need to set the expiration date of cookie in the past with the name of cookie value. The syntax is :
 setcookie("Cookie_Name", "", time()-3600);
The above statement will set the cookie expiration time back in one hour past. Example :
<?php

  setcookie("user", "", time()+3600);

?>
Also note that in a php file, cookies will be created before the <html> tag, otherwise it may cause some errors. For example :
<?php

  setcookie("user", "John wick", time()+(60*60*1), "/", "localhost");
  setcookie("age", 50);
  setcookie("email", "Johnwick@killer.net");

?>

<html>
<body>
    
<?php

  echo $_COOKIE['user'] . "<br/>";
  echo $_COOKIE['age'] . "<br/>";
  echo $_COOKIE['email'] . "<br/>";

?>

</body>
</html>
Output :

John wick
50
Johnwick@killer.net


Next Topic :