• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rahul Bodana

Blogger, Programmer & Trader

  • Code
  • Charm
  • Money
  • Write
Home » Code » PHP » PHP Cookies: What is, How to Create, Modify, Retrieve & Delete?

PHP Cookies: What is, How to Create, Modify, Retrieve & Delete?

April 16, 2022

Using PHP you can Create, Modify, Retrieve, and even delete cookies. Do you want to know what cookies you can use in your PHP code?

What is Cookie?

Cookies are small files that are embedded in the user’s computer from a website.

It is used to identify a user when the client request a page this will send a cookie too.

Using PHP we can easily create and retrieve values stored in cookies.

Cookies are essential for websites and give webmasters information about their visitors.

For example, depending on websites it is used for tracking user login information, shopping cart, browser information, and so on.

You can call cookies text files that contain user-specific information and are created upon connection by the server.

How to Create cookies with PHP

Using the function setcookie() we can set cookies.

Here is the syntax for this function:

setcookie(name, value, expire, path, domain, secure, httponly);

Except for the first parameter “name”, all parameter in this function is optional.

So if we want to create cookies using this syntax, here is how it’s done:

# Defining cookie variable:

$cookie_name = "user";
$cookie_value = "Rahul Bodana";
$cookie_duration = time() + (86400 * 30);

# Setting up the cookie:

setcookie($cookie_name, $cookie_value, $cookie_duration);

Here is the explanation for the code above:

  • In cookie_duration variable we set the duration from current time to next 30 days. It means it’ll expire after 30 days once set.
  • Please note down that The setcookie() function must appear BEFORE the tag, in HTML file.

How to Retrieve a Cookie in PHP

We learned how to set cookies using the PHP function setcookie(), the next step is obviously to learn how to retrieve that cookie.

Here we would not only retrieve cookies using the cookie name but also check that the cookies are set properly.

isset() the function is used to check whether the cookies are set or not.

Here is the complete example:

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

How to Modify a PHP Cookie

Modifying the cookie is not different from setting up the cookie.

To modify a cookie we just set it again with the values that we want to replace.

Here is how it can be done:

# Defining cookie variable:

$cookie_name = "user";
$cookie_value = "Sarvin Batra";
$cookie_duration = time() + (86400 * 30);

# Setting up the cookie:

setcookie($cookie_name, $cookie_value, $cookie_duration);

How to Delete a Cookie

Deleting cookies is a little bit tricky but not too complex.

The trick is simple if you want to delete a cookie all you need to do is set the time in the past.

For example, if you set the cookie expiration time even 1 minute ago from the current time, it would be deleted.

Here is how can delete it:

setcookie("user", "John doe", time() - 3600);

In one minute there are 60 seconds so in one hour it is 3600 seconds (60 * 60).

Using the (-) symbol with 3600 and time() function, we set the expiration time 1 hour in the past.

This code would delete that particular cookie.

How to confirm if Cookies are enabled?

Well, we learned What are cookies, how to create cookies, how to retrieve cookies, how to modify them, and even how one can delete cookies.

But what about making sure that cookies are enabled and working correctly.

Well, we don’t need a specific function to achieve that task since we could achieve that result by simple conditionals:

if(count($_COOKIE) > 0) {
  echo "Cookies are enabled.";
} else {
  echo "Cookies are disabled.";
}

If cookies are set it would echo that cookies are enabled otherwise disabled message.

Share this:

  • Twitter
  • Facebook
  • Reddit
  • Pinterest
  • WhatsApp
  • More
  • Pocket
  • Telegram

Related Articles

Suggested Posts:

  • PHP Session: How to Start, Get, Modify, and Destroy PHP…
  • PHP File Upload: Creating HTML form, and PHP Upload Script
  • PHP File Handling: How to Open, Read and Close files using…
  • PHP Filters: How to use filters for Validation &…
  • PHP Date Function: How to use Date & time function with…
  • JSON in PHP: How to Encode, Decode and Retrieve all JSON…
  • PHP Inheritance: Concept, Override method, Modify & Final…

Posted in: PHP

Primary Sidebar

Recent Articles

  • PHP Inheritance: Concept, Override method, Modify & Final keyword
  • PHP Access Modifiers: Types of Specifiers, How to Use with Examples
  • PHP Constructor and Destructor: How to Create in PHP With Examples
  • PHP Classes & Objects: How to Create Classes and Objects in PHP
  • PHP OOP: What is OOP in PHP, Why use, PHP Classes and Objects
  • Evolution of Computers: History, Timeline, Ancient & Modern Computing devices
  • Introduction to Computer: Definition, Need & Functions of Computer

Copyright © 2022 by Rahul Bodana