Have you ever wondered why a website remembers that you're logged in, even after refreshing the page?

The answer lies in cookies and sessions.

The problem with HTTP

HTTP is stateless. This means that each request you send to a server is independent. By default, the server doesn't remember who you are from previous requests.

So how do websites keep you logged in?

What are cookies?

Cookies are small pieces of data stored by your browser. They can help websites remember information such as your session identifier, preferences and shopping cart.

What are sessions?

In a typical server-side session system, the server stores information about your authenticated session and gives your browser a unique session identifier.

Whenever you make another request, your browser sends that identifier through a cookie, allowing the server to recognize your session.

How they work together

  1. You log in to a website.
  2. The server verifies your credentials.
  3. The server creates a session.
  4. Your browser receives a cookie containing the session ID.
  5. Subsequent requests include the cookie, allowing the server to recognize your session.

Watch the animated explanation

I've created a beginner-friendly animated video explaining how cookies and sessions work together, without unnecessary technical jargon.

Final thoughts

Cookies and sessions are fundamental concepts in web development. Understanding them makes it easier to learn authentication, authorization and web security.

WattaDumb Explains — Making complicated technology simple.