PHP Cookie

PHP Cookie are used to identify the old user. Cookie is a small file. Whenever you visit a website, that website stores cookies on your PC.

After a few days when you request a browser back to the same website, the request of the website is also sent to the cookie. Through cookies, that website knows that you have already visited the website.
php Cookie are mainly of 2 types.

  1. Analytical cookies – With such cookies you can identify an old visitor and also find out the number of visitors.
  2. Functional cookies – These types of cookies are used to perform functional tasks such as the user’s old choices, you can find out through such cookies.

Why use Cookie ?

  • To improve the user experience.
  • To identify the user.
  • Number of visitors to count
  • Remember the user’s old choices.

Create PHP Cookie

To create cookie, you use the setcookie () function. This function has 6 parameters. This function format is give below.

setcookie(name, value, expire, path, domain, secure, httponly);
  • name-of-cookie – This is the name of the php cookie. This is an unique name. The name of the cookie is accessed by the same name.
  • value – This is the value you want to store. These values can be anything like a string or integer value etc.
  • expire-time – This is the time until you want to store the cookie.
  • path – This is the path of the directory where you want to store the cookie.
  • domain-name – This is the name of the domain. If your website is large then you can use its name here.
  • security – If you set it to 1 then the cookie can only be sent through secure HTTPS.

Example

<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
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];
}
?>

Modify a Cookie Value

Cookies in PHP are modify with the help of some setcookie() functions.

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
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];
}
?>

Accessing Cookies

The $ _COOKIE [] array are store cookie in php. If you want to access the value of a single php cookie, you can access it via $ _COOKIE [‘cookie-name’] variable.

Example

<?php
if(isset($_COOKIE[“name”]))
{
// Accessing cookie
echo “Good morning”.$_COOKIE[“name”];
}

?>

Deleting PHP Cookie

You can also delete cookies after using cookies. There are two ways to do this.

  • Call the setcookie () function only with the name of the cookie.
  • Call the setcookie () function with a date of the past.

Example

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>