PHP cookies- To manage user state

HTTP is stateless. The variables used in one page will not be available in the next pages of application unless preserved through sessions. Each page is treated as an individual transaction by the server. Each page is independent of the previous transactions. Programming languages used to create web applications offer Cookies in addition to Sessions to maintain the states of pages. State management is essential to create relationship among webpage transactions. In this post we will learn about PHP cookies for managing user state to create interactive web applications.


A cookie is a text file of a very small size and is saved in the system of the user of a website. The path and location is visible in the following image.

Cookies Storage in a system

The data in this file is about user behavior and user preference along with the owner website and expiry date is set. This stored data is retrieved by the website whenever user visits it again. It is just like recognizing a user by a website in all her forthcoming visits.

Note- Cookies can be only set when the user allows the website to do so. You can permit a website or reject the request when you visit a website and a pop-up displays that it uses Cookies. Every website using cookies mandatorily informs the visitors about the use of Cookies.

The image below is an example of how a website may present its cookie policy to its visitors.

Cookie Policy

Use of PHP Cookies

The cookies are most commonly used to retain the personal preferences of a user for a specific website.  These are stored in the local machine of the user. When the user returns back to the same website again, the information retained in cookies is used to give same experience to the user on the website. The shopping carts, login interfaces, dashboards and other portals where users register also use cookies.

The websites using the cookies often inform the website visitors about use of cookies since their information will be stored. The cookies are used to store user specific information, store and access this data, use it form manipulation of web application and customize the headers or other user-specific presentation.

Other examples of cookies can be to display the preferred look and feel of a website as previously opted by a visitor. The PHP cookies are also used to maintain the information about what pages of a website were viewed by the user.

Are Cookies Harmful to website users?

As a user I may fear or think once to allow a website to set cookies on my system. The harmfulness of any technology lies in the user of the technology. In case of cookies the danger completely depends upon the website owner or administrator and his intention. The following features of cookies make them a trustworthy piece of information stored in your system.

  • Cookies are available to be accessed by those websites which created them in your system. Cookies allow transfer of data between your system and its parent domain.
  • Maximum number of allowed cookies per domain in one system is 20
  • Maximum allowed size of a cookie is 4kb
  • A system can have maximum 300 cookies in total to be set by all domains.
  • Turning cookies off is a system owner’s prerogative. If user has set the option to turn off cookies, no domain can turn it on and set the cookies

Be First to Comment

Leave a Reply

Your email address will not be published.