PHP Cookies – How to set, read and delete a cookie in PHP?
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, You are going to learn what is a cookie and How to create, set, read and delete a cookie in PHP.
What is Cookie?
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.
Why Cookie?
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.
PHP Cookies
I have explained what is a cookie and their importance. Let’s learn how to set a cookie in PHP.
In PHP setcookie() method is used to set a cookie in a user browser.
Syntax to set a cookie in PHP–
1 |
setcookie(name, value, time, path, domain, secure); |
name – Defines the name of a cookie.
value – What value we want to store in a cookie. A Cookie always stores a string value.
How to store an array value in a cookie
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) – Set the domain in which a cookie is available. If you want to set a cookie for multiple subdomains, then use the name of main domain with a dot as a prefix.
For example –
Suppose webrewrite.com has multiple subdomains such as tech.webrewrite.com, education.webrewrite.com. And I want a cookie set by webrewrite.com can be accessible to all of my subdomains then I set the domain as .webrewrite.com.
secure (optional field) – It Specifies whether or not a cookie should 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.
1 2 3 |
/* Create cookie with setcookie method. */ setcookie("firstcookie","studentduniya"); |
A Cookie is created with the name of a firstcookie with a value of studentduniya.
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 from a cookie
NOTE: I have not set the time of this cookie, so it will expire when I close my browser.
With a 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 usingMozilla then you can use firebug.
We have not defined the expiration time of a cookie. Let’s set a cookie for 30 days. In this case, We need to mention the time, so that cookie still remain for 30 days until a user deleted them manually.
1 2 3 |
/* Set cookie for 30 days. */ setcookie("firstcookie","studentduniya",time()+3600*24*30); |
How to set a cookie for multiple subdomains
1 |
setcookie("firstcookie","studentduniya",time()+3600*24*30,"/",".webrewrite.com"); |
How to Get the Value of a Cookie in PHP
In PHP, $_COOKIE is used to get the value of a cookie. So how do we read the value of a cookie which we set earlier?
1 2 3 |
$value = $_COOKIE['firstcookie']; echo $values; // It echo studentduniya |
How to Delete a Cookie in PHP
To delete a cookie in PHP, set the negative time (past time).
1 |
setcookie ("firstcookie","studentduniya",time()-3600*24); |
So what happens if we set negative time. In that case, a cookie will expire automatically.
Important Points About Cookies
1. Cookies can store maximum 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 a Mozilla browser.
4. Try to avoid storing sensitive and confidential data in cookies.
Recommended Books for PHP
Conclusion
I tried my best to explain the concept of a PHP cookies. If I miss any point or you want to add something you can let me know by your comments.