What is Cookie, Session and LocalStorage – Quick Overview
Cookie
Cookies are small text files stored on ClientSide. All browser cookies have a maximum capacity of 4 KB. We don’t need a function to start a cookie. Cookies store continuous user verification data on websites. We may enable or disable cookies on a particular site. This helps to create simple and fast website information. With User Data Cookies Provide Better User Information, Website Access, and Quick Access.
We can store a maximum of 20 cookies per site. It is a good idea to clear cookies from time to time for certain issues, such as uploading or formatting sites.
When the browser submits a request to the webserver the following time, it sends the cookie information to the server, which the server uses to identify the user.
There are many types of cookies:
- Session Cookie
- Permanent cookies
- Third-party cookies
Session Cookie
A session cookie is a server-specific cookie that cannot be transmitted to any machine other than the person who generated the cookie. A session cookie does not have an expiration date. Time cookies are stored in memory and are never written to disk. If the browser closes, the cookie will be permanently deleted.
Permanent cookies
Permanent cookies are also known as persistent cookies, the Permanent cookie are deleted after the Browser Lock is only deleted when the User sets a cookie expiration date.
Stay active even when the web browser is closed.
Third-party cookies
Third-party cookies are set by third-party companies to collect certain types of information from web users for research purposes, for example, behavior, demographics, or demolition practices. They are often used by advertisers to show the most relevant ads between websites aimed at the right audience.
Example:
var cookieName = 'test1'; var cookieValue = '1'; var myDate = new Date(); myDate.setDate(myDate.getDate() +30); document.cookie = cookieName + "=" + cookieValue + ";expires=" +myDate;
Session
Session Stores User information on the server-side For a while. There is no Session Storage Limit. Sessions are used to transfer values from one page to another. The session is sometimes dependent on Cookie. Server times are mostly used in larger web applications, which require a lot of user data to be stored. Each session is assigned a unique id that is used to retrieve saved values. Whenever time is created, a cookie containing a unique session id is stored on the user’s computer and is returned with all requests to the server.
Each session is unique to each user, and there is no limit to the number of sessions that can be used in a single application.
It is more secure because it is stored on the server, we cannot easily access it. It embeds cookies on the user’s computer. It stores unlimited data.
A session ends when the user quits the browser or leaves the site, or when the server terminates the session after a predetermined amount of time, usually 30 minutes.
Local storage
This is because LocalStorage has many advantages over cookies. One very important difference is that, unlike cookies, data does not have to be forwarded to every HTTP request. This reduces the congestion between the client and the server and the amount of bandwidth lost. This is because data is stored on the user’s local disk and is not destroyed or erased to lose internet connection. Also, as mentioned earlier, LocalStorage can handle up to 5MB of data. This is far more than the 4KB cookies it contains.
localStorage is insecure for storing sensitive information that can be accessed using any code. So, it is quite insecure.
LocalStore behaves like continuous cookies with expiration. Data is not automatically destroyed unless it is erased with Javascript code. This can be good for larger bits of data that need to be stored longer.
Example:
localStorage.setItem('Name','Abc'); alert(localStorage.getItem('Name'));
Cookie vs Session vs LocalStorage
Cookies
- There are several expiration dates (both the server or client can set up expiration date)
- The Client can’t access the Cookies if the HttpOnly flag is set to true.
- Has SSL Support.
- Each HTTP request sends data to the server.
- The maximum file size is 4 KB.
Local Storage
- Has no expiration date.
- Client only.
- no support for SSL.
- Data are not transferred on every HTTP request.
- 5 MB limit ( 5 megabytes).
Session Storage
- If we close the browser or close the tab, session data will be removed automatically.
- Client only.
- No support for SSL.
- Data are not transferred on every HTTP request.
- 5-10 MB ( megabytes ) limit.
- It’s synchronous.
Leave a Reply
Want to join the discussion?Feel free to contribute!