This is definitely going to be an interesting read for our readers as this has been asked to and by every programmer at one point of time or other and trust us less than 5% know the answer! Also we are going to break down these tough words in easiest language everrr!!! So, no more same repetitive answer you see everywhere because lets clear this once an all what are cookies, session storage, local storage for all finally. We are going in depth but like always would keep it short and sweet.
First what is client side Storage?client-side storage means data is passed to the browser’s storage API, which saves it on the local device in the same area as it stores other user-specific information, e.g. preferences and cache. Now, lets break it down further. every website, web app or portal you scroll, you use a browser right! Be it chrome, Internet Explorer, Firefox any. When a website a cookie or session or local storage it uses brower’s API where they send parameters and depending on the request sent to browser it is stored locally on person’s device. So, here application is not storing on sql server etc database but on the client’s system.
Now, lets dive straight into detail of each.
What are cookies?An HTTP cookieis a small piece of data sent from a website and stored on the user’s computer by the user’s web browser while the user is browsing.Cookies record the user’s browsing activity and also to remember names, addresses, passwords, and credit-card numbers.
where cookies are stored in the system?Google Chrome stores all cookies in a single file called Cookies. The file is located at the following path: “C:\Users\Your User Name\AppData\Local\Google\Chrome\User Data\Default.”
what does Incognito mode do?When you use incognito mode, Chrome doesn’t record any history or cookies, and it disables browser extensions.
how to create a cookie?
The Set-Cookie HTTP response header sends cookies from the server to the user agent. A simple cookie is set like this:
2. What are Session and Session Id
Again to keep user information intact while he is using the portal we use http sessions. Now, session data is store on the client side about say username, password etc but the session id is stored on the cookies that is on client side. Now, once a user ends a session say closes a window or tab or browser then session id is deleted. While the user stays logged in, the cookie would be sent along with every subsequent request.
But now a days nobody uses sessions just java application use them! People prefer JWT . In the token based application, the server creates JWT with a secret and sends the JWT to the client. The client stores the JWT (usually in local storage) and includes JWT in the header with every request. The server would then validate the JWT with every request from the client and sends response.
3. What is local Storage and how it is different from cookies?
Cookies are mainly for reading server-side, whereas local storage can only be read by the client-side. Apart from saving data, a big technical difference is the size of data you can store, localStorage gives you more data to store as size of a cookie is just 3 KB! So, have you quite find the catch here? The thing is if your application requires something that is stored on the device, some data which is usually some files required to run the application etc then you go to local storage.
Hope this tiny read would clear all your doubts forever❣