Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage vs sessionStorage vs cookies

I am working in an app where I need to keep some data during the user is logged in and I have that question, what is the difference among localStorage, sessionStorage, cookies ???

I was asking what can I use in order to persist some data in the DOM, even if the user refresh the page, some people says: use sessionStorage, or localStorage, then, someone came up with the idea of use ngCookies because it works in every browser, but, which should I use ?

like image 393
Non Avatar asked Apr 30 '15 05:04

Non


People also ask

Should I use cookie or local storage?

If you need to store data that is accessible for both the server and the client, use cookies. Otherwise, use local storage. If you need to store larger data, use local storage. If you need to store data that does not expire, use local storage.

Which is safer cookie or localStorage?

Conclusion. Both cookies and localStorage are vulnerable to XSS attacks. However, cookie-based token storage is more likely to mitigate these types of attacks if implemented securely. The OWASP community recommends storing tokens using cookies because of its many secure configuration options.

Should I use localStorage or sessionStorage?

localStorage and sessionStorage are almost identical and have the same API. The difference is that with sessionStorage , the data is persisted only until the window or tab is closed. With localStorage , the data is persisted until the user manually clears the browser cache or until your web app clears the data.


1 Answers

localStorage and sessionStorage are both so-called WebStorages and features of HTML5.

localStorage stores information as long as the user does not delete them.

sessionStorage stores information as long as the session goes. Usually until the user closes the tab/browser.

cookies are simply cookies, which are supported by older browsers and usually are a fallback for frameworks that use the above mentioned WebStorages.

In contrast cookies can store way less information then WebStorages and the information in WebStorages is never transferred to the server.

Keep in mind that the EU has a regulation that requires websites to inform their users about the usage of cookies. I dont know whether this also applies to WebStorages

like image 88
Jonathan Avatar answered Nov 09 '22 15:11

Jonathan