A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user’s selected product) to be remembered from one page to the next.
Using session and cookie, it is very helpful to keep track every movement of the visiting web page information.
What is Cookie?
- Cookies are usually small text files that are stored on your computer’s browser directory or program data subfolders.
- A cookie will typically contain the name of the domain from which the cookie has come, the “lifetime” of the cookie, and a value, usually a randomly generated unique number.
- The cookie variables are stored in the client machine, it can be available till it gets deleted either by browser setting, or by the code or by the user manually.
Say for example, if a person has username and password to login a website, this can be set as a cookie in their browser so they don’t have to remember the username and password to login the website when they visit again. The person can block or delete the cookies at any time.
What is Session ?
- Session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc).
- However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.
- Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID.
- This helps to prevent two users’ data from getting confused with one another when visiting the same webpage.
The main difference between session and cookie is that cookie’s are stored in the browser while sessions are stored in the server.
Learn more about Session and cookie