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.
setcookie(name, value, time, path, domain, secure);
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 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 subdomain then I set the domain is .webrewrite.com.
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.
/* Create cookie with setcookie method. */
A Cookie is created with the name of a firstcookie and it’s value is 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.
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.
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.
/* Set cookie for 30 days. */
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).
$value = $_COOKIE['firstcookie'];
echo $values; // It echo studentduniya
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
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.