---
title: "What is a cookie?"  
description: "A Cookie is a text file that the server embeds on the user’s computer. A cookie is used to identify a user. These cookies are also used for tracking p"  
author: "zack mathews"  
published: 2016-06-07  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11143/what-is-a-cookie  
category: "php"  
tags: ["php", "cookies"]  
reading_time: 2 minutes  

---

# What is a cookie?

A Cookie is a [text file](https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file) that the server embeds on the user’s computer. A cookie is used to [identify](https://www.mindstick.com/forum/159425/java-app-crash-arrayindexoutofboundsexception-identify-invalid-index) a user. These cookies are also used for tracking purpose.PHP [supports](https://www.mindstick.com/blog/12043/how-to-create-a-killer-mobile-app-that-supports-your-brand) HTTP cookies. These cookies are usually set in HTTP header. With the help of PHP you can create and delete the cookie.\

##### Create a cookie with PHP

**setcookie()**[function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is used to create a cookie. This function has six [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp):

**Name:**This [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) is the name of the cookie and store in [environment](https://yourviews.mindstick.com/view/308/need-to-create-a-supportive-work-environment-for-women) [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) called HTTP_COOKIE_VARS.

**Value:** It is used to store the content of the cookie.

**Expiry:** If this parameter is not set than cookie will automatically expire when the browser is closed.

**Path:**This parameter specifies the directories for which cookie is valid.

**Domain:** All cookies are valid for only those host and domain, who had created them.

**[Security](https://www.mindstick.com/articles/43813/new-security-technologies):**This can be set to 1 to specify the [secure transmission](https://www.mindstick.com/blog/303593/data-fortress-unlocking-the-power-of-tls-ssl-for-secure-transmission) using HTTPS otherwise set 0 for cookie to be sent by regular HTTP.

##### Syntax:

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

```
<?php   setcookie("name", "Neha singh", time()+3600, "/","", 0);   setcookie("salary", "35000", time()+3600, "/", "",  0);   echo "Set Cookies"?>
```

##### Output:

Set Cookies

##### Modify a cookie

To modify a cookie use the same name and function setcookie():

```
<?php
setcookie("name","Raghu raj");//Neha singh is replaced by Raghu raj.
if(!isset($_COOKIE["user"]))
{
echo "Cookie is not set.";
}
else
{
echo "Cookie is set.";
}
?>
```

##### Output:

Cookie is set. \

##### Delete a cookie

To delete a cookie use setcookie() function with expiration time:

```
<?php
// set the expiration date to one hour ago
setcookie("name", "", time() - 3600);//name cookie will be deleted
echo "Cookie 'name' is deleted.";
?>
```

##### Output:

Cookie 'name' is deleted.\

---

Original Source: https://www.mindstick.com/blog/11143/what-is-a-cookie

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
