What is the difference between a session and a cookie in terms of storage location and security, and why shouldn't you store sensitive data in a cookie?

Last updated 15 June 2026

A cookie is stored on the client (the browser) and sent with every request, while a session stores data on the server and uses only a small session ID cookie to link the client to it. Because cookies live on the user's machine and travel over the network, they are easy to read or tamper with, so sensitive data belongs server-side in the session.

  • Storage location:

    • Cookie: stored in the browser, sent in the Cookie header on each request to that domain.

    • Session: data kept on the server (files, Redis, DB); the browser only holds the PHPSESSID identifier.

  • Security implications:

    • Cookies are user-controlled: readable, editable, and forgeable unless signed/encrypted.

    • Cookies are exposed to JavaScript (XSS) unless HttpOnly, and to network sniffing unless Secure.

    • Session data never leaves the server, so it can't be tampered with directly.

  • Why not store sensitive data in a cookie:

    • A cookie holding a role like is_admin=1 can simply be edited by the user.

    • Store an opaque session ID in the cookie and keep the real data (user ID, permissions) server-side.

  • https://www.geeksforgeeks.org/ethical-hacking/what-is-cookie-hijacking/
  • https://www.geeksforgeeks.org/php/php-sessions/
  • https://www.tutorialride.com/php-interview/php-interview-questions-and-answers.htm
  • https://www.geeksforgeeks.org/php/what-are-the-difference-between-session-and-cookies-in-php/
  • https://www.guru99.com/difference-between-cookie-session.html
  • https://www.scribd.com/document/514322838/py
  • https://stackoverflow.com/questions/5608764/purpose-of-php-sessions-and-cookies-and-their-differences
  • https://www.youtube.com/watch?v=xUPyxyXnx0o
All PHP interview questions