Cookies in PHP – Create, Set, Read and Delete a Cookie

HTTP is a stateless protocol. It means user sends a request, a server responds to that request without remembering the request later. To track important information (such as user preference, user activity etc.) on a website cookies are very useful. In this blog post, I’ll explain what is cookies. How to create, set, read and delete a cookie in PHP.

what is Cookies

Cookies are very useful to store small information in a remote browser (user browser). It’s a simple text file which stores maximum 4KB of data.

Cookies are created by a browser on the request of a server (as per program logic). Once a cookie is created on a user browser, the browser sends that cookie information back to the server using HTTP Header.

Importance of Cookies

HTTP is a stateless protocol, so there is no way to track user’s previous activities on a website. Cookies are the best way to store the stateful information like the number of time user visited on a website, product viewed by a user, their preference etc.

Cookies in PHP

I hope you’ll get the idea about cookies and their importance. Let’s see how to create cookies in PHP.

In PHP setcookie() method is used to set or create a cookie.


name – Defines the name of a cookie.

value – What value you want to store in a cookie. Cookies always store string value. If you want to store an array value in a cookie the read my previous post on How to store array value in cookies.

time (optional field) – Set the cookie expiration date. If this field is empty then the cookie will be deleted when a browser is closed.

domain (optional field) – You can set domain in which a cookie is available. If you want to set a cookie for multiple subdomains, then use the name of the main domain with a dot as a prefix.

Suppose has multiple subdomains such as,  And I want a cookie set by can be accessible to all of my subdomain then I set the domain is

secure (optional field) – It Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. By default secure option is FALSE. If it is TRUE it means cookie will only be set if a secure connection exists.

A Cookie is created with the name of a firstcookie and it’s value is studentduniya.

Subscribe Our Tutorials

Get Latest Updates on Facebook

NOTE – A Cookie can only store string values. It can’t store an array. To store an array in a cookie, you need to first convert them into a string.

How to store & retrieve an array value in a cookie

NOTE: I have not set the time of this cookie, so it will expire when I close my browser.

Once you execute a PHP script with setcookie method a cookie will be set.

Once a cookie is set, you can check the cookie on a chrome developer tool or if you are usingMozillaa then you can use firebug.

Cookies in PHP

Cookies in PHP


Let’s set a cookie for 30 days. In this case, We need to mention the time, so that even if a user close a browser cookie still remain for 30 days until a user deleted them manually.

How to set a cookie for multiple subdomains

Read the Value of Cookies in PHP

In PHP $_COOKIE is used to read the value of a cookie. If I want to read the value of firstcookie (a cookie which i have created).

Delete a Cookie

To delete a cookie, set the negative time (past time).

So what happens if we set negative time. In that case, a cookie will expire automatically.

Important Points About Cookies

1. Cookies can store up to 4 KB of data.

2. It can only store a string.

3. Cookies can only be accessed by the browser that set them. Cookie set by chrome browser cannot be accessed by Mozilla browser.

4. Try to avoid storing sensitive and confidential data in cookies.

Recommended Books for PHP

PHP Books on Flipkart

PHP Books on Amazon India


I tried my best to explain the concept of a cookie. If I miss any point or you want to add something you can let me know by your comments.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.