PHP Cookies

Cookies are basically text files which are stored on client computer to track the user activities.

What is Cookies in PHP

Cookies are basically text files which are stored on client computer to track the user activities. PHP transparently supports HTTP cookies. So a cookie is a small file with the maximum size of 4KB that is storedon the client computer.

How to create cookie in PHP

In PHP, you can set a cookie using the functions setcookie() or setrawcookie(). As cookies are the part of HTTP header so setcookie() should be called before any output is sent to the browser.

Syntax of creating a cookie in PHP

All parameters details are given below:
  • Name − The name parameter is compulsory for creating a cookie and only this variable name will be used while accessing any cookies.
  • Value − This parameter will be used to set the value of the cookie variable name.
  • Expiry − Using this parameter, we can also set time of expiry of any cookie. And after the specified time period the cookie will be automatically be deleted from the client computer. And if the expiry parameter is not defined then the cookie will be automatically expired when the browser is closed.
  • Path − In this parameter we will specify the directories for which the cookie is valid. If we add “/”, it means the cookie is valid for the whole website.
  • Domain − In this parameter, just pass the domain name for which you want the cookies to be used for example www.google.com
  • Security − Just set this to 1 if the cookie is only sent by a secure transmission using HTTPS otherwise keep it to 0.
NOTE: WHILE CREATE A COOKIE IN PHP, THEN ONLY THE “NAME” PARAMETER IS COMPULSORY. SO REST OF THE PARAMETERS COULD BE BLANK.
Example of creating a cookie in PHP

How to delete a cookie in PHP

If you want to delete a cookie then you have to define the “expire” parameter with the expiry date.

EXAMPLE OF DELETING A COOKIE IN PHP

What is the difference between Session and Cookie in PHP

As we may have some confusion in sessions and cookies because we may think that both are working for the same purposes. But its not true, there are differences in sessions and cookies. Here are the main differences:

  1. Sessions store data on server side instead cookies stores the values in browser only.
  2. Sessions are used to store more sensitive and important information because sessions are more secure instead cookies are not secure because all cookies values are stored in the user’s browser only and anyone can check these values.
  3.  Sessions needs extra space on server because it creates a file when session is created and that file keeps all the sessions ids. Most commonly /temp folder is used for this purpose. instead cookies don’t needs any extra space because it just created in the browser only.
  4. If we want to maintain sessions on all the pages then we have to use the functions session_start() otherwise we will not get the session value on all the pages instead we don’t need to use any extra function to maintain its values on all pages, we can just use the global variable $_COOKIE to access the cookies values.

We hope we explained sessions and cookies both very well and you understand the differences.

Leave a Reply

Your email address will not be published.