In this tutorial, we will be learning how to use cookies with PHP.
What Are Cookies?
Even if you're not a programmer, the word cookie should be something you've already heard of.
You can think of cookies simply as it stores data temporarily.
A real-life use of cookie happens everywhere we shop online.
If it were not possible to use cookies, we would be unable to store items in our own shopping cart.
The problem would be every time we click a new item page, our shopping cart would instantly forget what items we added earlier.
This happens because our HTTP is stateless. Every connection to a web page is independent and has nothing to do with a previous connection.
And previous data stored in the browser will be forgotten.
Cookie is a technology that we use to store data in the client side temporarily.
When our code request that client stores, a cookie, so long as their browser supports cookies(and they haven't turned off the feature), we can store some of their data temporarily.
Every time you access a different page(including when you go back to it via the back button) the data can be retrieved via a cookie.
Because cookies are stored at the client's side, as long as the user doesn't delete the cookies, the cookies can be kept for a set period of time…they will not disappear just because you close the browser.
As we mentioned before, the most common use of cookies is in online shopping. Many shopping carts use cookies.
Creating And Updating A Cookie In order to create a cookie we use the setcookie() function, the basic syntax is: setcookie(name, value, expire); Name: name of the cookie Value: the cookie variable's value Expire: the expiry date of the cookie The name and field are required fields, while the expire field is optional. The reason the expiry date exists is to cancel out the cookie after it a set amount of time.
Example
<?php
setcookie("counter", 1, time() + 20*60);
echo $_COOKIE["counter"];
?>
Result This creates a cookie with a name of counter and a value of 1 And it will expire in 1200 seconds or 20 minutes (the number value is in seconds). Example
<?php
setcookie("counter", 2, time()+20*60);
echo $_COOKIE["counter"];
?>
This updates the value of the cookie name counter to 2. Example
<?php
setcookie("counter", 2, time()+20*60);
echo $_COOKIES["counter"];
echo "<br>";
setcookie("counter2", 50, time()+20*60);
echo $_COOKIES["counter2"];
?>
The name of the cookie is "counter2" and its value is 50. Result
As you can see, you can use setcookie()
to both create and update the value of a cookie.
Reading and Deleting A Cookie
So now that we've created our cookie, we can read out its value.
If you wish to read value, we use our cookie array.
All of our cookie values are stored in a cookie array.
The syntax would be <?php echo $_COOKIE["name"]; ?>
If we wish to delete a cookie, we simply set its value to "" or FALSE. Or we set the cookie's expiry data to a day in the past. After you close your browser, you will see that your screen largely remains the same.