Introduction to State Management
HTTP is a stateless protocol which means every user request is processed independently and it has nothing to do with the requests processed before it. Hence there is no way to store or send any user specific details using HTTP protocol.
But in modern applications, user accounts are created and user specific information is shown to different users, for which we need to have knowledge about who the user (or what he/she wants to see etc) is on every webpage.
PHP provides for two different techniques for state management of your web application, they are:
- Server Side State Management
- Client Side Server Management
Server Side State Management
In server side state management we store user specific information required to identify the user on the server. And this information is available on every webpage.
In PHP we have Sessions for server side state management. PHP session variable is used to store user session information like username, userid etc and the same can be retrieved by accessing the session variable on any webpage of the web application until the session variable is destroyed.
Client Side State Management
In client side state management the user specific information is stored at the client side i.e. in the browser. Again, this information is available on all the web pages of the web application.
In PHP we have Cookies for client side state management. Cookies are saved in the browser with some data and expiry date(till when the cookie is valid).
One drawback of using cookie for state management is the user can easily access the cookie stored in their browser and can even delete it.