What is the difference between a Session and a Cookie in PHP? How does PHP track a session across multiple requests?

Last updated 15 June 2026

A cookie is a small piece of data stored in the browser and sent with each request; a session is server-side storage for user data. PHP links them by storing only a session ID in a cookie and keeping the actual data on the server.

  • Cookie: client-side: Stored in the browser, sent on every request to the domain; visible and editable by the user, so never trust it for sensitive data.

  • Session: server-side: Data lives on the server (files, Redis, DB); the client only holds an opaque identifier.

  • How tracking works:

    1. session_start() generates a session ID and sends it in the PHPSESSID cookie.

    2. On each later request the browser returns that cookie, and PHP loads the matching $_SESSION data.

  • Security notes: Use HttpOnly, Secure, and SameSite flags, and call session_regenerate_id() after login to prevent fixation.

  • https://www.php.net/manual/en/book.session.php
  • https://www.php.net/manual/en/session.idpassing.php
  • https://www.geeksforgeeks.org/php/what-are-the-difference-between-session-and-cookies-in-php/
  • https://medium.com/@folasayosamuelolayemi/mastering-session-management-in-php-a-guide-to-storing-and-passing-user-data-between-pages-04f3ea893515
  • https://www.geeksforgeeks.org/php/php-sessions/
  • https://www.php.net/manual/en/session.examples.basic.php
All PHP interview questions