PHP Cookies: How to Create, Modify & Delete Cookies

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 8, 2025
Edited by Kamila

Edited by Kamila

Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

Manage web sessions effectively with PHP’s powerful cookie handling capabilities. Explore these programming examples to understand how to Create, modify or delete PHP Cookies:

In this tutorial, you will learn what a cookie is, how to create, modify & delete PHP cookies, and how to verify if cookies are enabled. Also, some frequently asked questions (FAQs) related to this topic are covered here.

Please note that we have used PHP version 7 in all examples.

Let’s begin!

=> Complete PHP Guide for ALL

PHP Cookies: A Practical Guide with Examples

PHP Cookies

What is a Cookie

A cookie is a small file that the server embeds on the client or the computer to identify a user.

A cookie can store information like:

  • IP addresses
  • Email addresses
  • Usernames
  • Passwords
  • Previous purchase details
  • Items viewed
  • Preferred settings like themes, etc.

Cookies are not used to identify you personally, but some information that cookies collect may be linked. A cookie can only hold a maximum of 4KB of data.

Furthermore, cookies are very useful for developers as well as computer users. Some of the advantages of cookies include:

  • Light-weight and occupies less memory
  • Easily configurable
  • Make browsing easier
  • Users can delete cookies

Creating a PHP Cookie

The PHP setcookie() function is used to create/set a PHP cookie. It has seven parameters that are listed below. Only the name parameter is mandatory.

  1. name – The name of the cookie
  2. value – The value of the cookie
  3. expires_or_options – the time the cookie expires
  4. path – The path (directory) which the cookie will be available on
  5. domain – the domain in which the cookie is available to
  6. secure – indicates that the cookie should be sent over a secure HTTPS connection from the client
  7. httponly – if true, the cookie is accessible only through the HTTP protocol

Furthermore, setcookie() must appear before <html>.

Syntax:

setcookie(
    string $name,
    string $value = "",
    int $expires_or_options = 0,
    string $path = "",
    string $domain = "",
    bool $secure = false,
    bool $httponly = false
): bool

The isset() function is used to check whether or not the cookie is set.

The superglobal, $_COOKIE is used to retrieve the cookie details like name, value, etc.

Let’s see an example. You can practice this example by running the following programming code:

<?php
 
$cookie_name = "Student";
$cookie_value = "David";
setcookie($cookie_name, $cookie_value, time() + (86400 * 7), "/"); 
 
?>
<html>
<body>
 
<?php
 
if(isset($_COOKIE[$cookie_name])) {
    echo "Cookie is set! Cookie name is $cookie_name and value is $cookie_value.";
} else {
    echo "Cookie is not set!";
}
 
?>
 
</body>
</html>

Note 1: In the above cookie example, we have used three parameters:

  1. name
  2. value
  3. expiration date – 86400 which indicates that the cookie will expire after 1 day. Similarly, you can use 86400 * 7 for 7 days, 3600 for one hour, 60 for one minute, etc.

The forward slash character (/) is used to indicate that the cookie is available on the whole website. If not, you have to specify the relevant path.

Note 2: After running the above programming code, refresh the page.

The below text shows the browser output of the above programming code (after refreshing the page).

Cookie is set! The cookie name is Student and the value is David.

Modifying a Cookie

Modifying a cookie is straightforward. You just need to set the cookie again to modify a cookie.

Deleting a Cookie

A cookie can be deleted using the setcookie() function with a past expiration date.

Let’s see an example. You can practice this example by running the following programming code:

<?php
 
//delete cookie
setcookie("student", time() - 60); 
 
?>
<html>
<body>
 
<?php
 
echo "Cookie is deleted!";
 
?>
 
</body>
</html>

How to Verify If Cookies are Enabled

The count() function and the $_COOKIE superglobal are used to verify whether cookies are enabled or not.

Let’s see an example. You can practice this example by running the following programming code:

<?php 
 
setcookie("test_cookie","test");
 
?>
<html> 
<body>
 
<?php 
 
if(count($_COOKIE)>0){
    echo "Cookies are enabled!";
}else{
    echo "Cookies are disabled!";
}
 
?>
 
</body>
</html>

Note: After running the above programming code, refresh the page.

The below text shows the browser output of the above programming code (after refreshing the page):

Cookies are enabled!

Frequently Asked Questions

1. What is a PHP cookie?

A PHP cookie is a small file that the server embeds on the client or the computer. It is used to identify users.

2. Where do cookies reside?

Cookies reside in your browser folder or subfolder.

3. Can cookies identify you personally?

Cookies do not identify you personally, but some information that cookies collect may be linked.

4. Do cookies collect IP addresses?

Yes, cookies can be used to collect IP addresses.

5. How much data can a cookie hold?

A cookie can only hold a maximum data capacity of 4KB.

6. How do you set cookies?

You can set a cookie using the PHP setcookie() function as shown below.
Example:
<?php
setcookie(“user”, “/”);
?>

7. What information is stored in PHP cookies?

Cookies can store information like email addresses, usernames, passwords, previous purchase details, items viewed, preferred settings like theme, etc.

Conclusion

A PHP cookie is a small file that the server embeds on the client or the computer to identify a user.

It can store information like IP addresses, email addresses, usernames, passwords, previous purchase details, items viewed, preferred settings like theme, etc. You can create, modify and delete PHP cookies.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



    Leave a Comment