In modern web development, cookies and local storage are essential tools...

What Are Cookies?

Cookies are small text files that a web server sends to a user's web browser...

Types of Cookies

  • Session Cookies: Temporary cookies that are deleted when the user closes their browser...
  • Persistent Cookies: These cookies remain on the user's device even after the browser is closed...
  • Secure Cookies: Cookies that can only be transmitted over secure HTTPS connections...
  • HttpOnly Cookies: Cookies that are not accessible via JavaScript...

Correct Usage of Cookies

Cookies are most commonly used for session management. They store session identifiers...

Potential Misuses of Cookies

Cookies can be used to track users across different websites without their consent...

What Is Local Storage?

Local storage is a feature of the Web Storage API that allows web applications to store data locally...

Correct Usage of Local Storage

Local storage is well-suited for storing user preferences, such as theme settings...

Potential Misuses of Local Storage

Local storage is not a secure place for storing sensitive information...

Anything a script on the page can read, an injected script can read too — which is why credentials, tokens and secrets do not belong there. The password generator on this site takes the same view from the other side: it never writes a generated password to storage of any kind, so there is nothing on the machine to steal after the tab closes.

Cookies vs. Local Storage

FeatureCookiesLocal Storage
Size Limit~4KB per cookie~5-10MB per domain
Data PersistenceSession or expiration-basedPersistent until explicitly deleted
TransmissionSent with every HTTP requestNot sent automatically with requests, only accessed via JS
SecurityCan be secured with HttpOnly and Secure flagsVulnerable to XSS, no built-in security features
Use CasesSession management, tracking, remembering user preferencesStoring large data, caching, offline usage

Best Practices for Using Cookies and Local Storage

  • Minimize Sensitive Data Storage
  • Implement Secure Cookies
  • Set Expiration Dates Wisely
  • Be Transparent with Users
  • Regularly Audit Data

Malicious Uses of Cookies and Local Storage

Cross-Site Scripting (XSS) Attacks: Attackers can inject malicious scripts into a web page...

Conclusion

Cookies and local storage are powerful tools in web development that can significantly enhance...

More reference topics

  • Infrastructure as Code — Three ways to define servers, networks and cloud resources in version-controlled text, and the sweet spot of each.
  • Learning to Script — The six things that reliably stall a new scripter, with worked examples of how to get past them.
  • Hypervisors — Comparing the three hypervisors you are most likely to run, plus optimisation and live video handling.
  • Linux — Choosing between the three most common server distributions, and the system knowledge that applies whichever you pick.
  • CI/CD Pipelines — The case for automating your build and deploy, the common ways it goes wrong, and what good practice looks like.
  • IoT — The open-source platforms worth knowing, and an honest look at what IoT delivers and what it risks.
  • GitHub — Pull, push, commit, merge, squash and branch — plus the conventions that stop a shared repository turning into a mess.
  • AI Practices — How to use AI coding assistants so they multiply what you can build, rather than handing you code you cannot debug.
  • Browser Stats — A live read-out of what this page can see about your browser — the same things every other site can.

Support Me

If you find my work interesting, please consider supporting me.