Search⌘ K

User Sessions

Explore the concept of user sessions in PHP and how they store short-term, user-specific data such as login status. Learn about session IDs, managing concurrency issues, and best practices for handling sessions in multi-server cloud environments. Understand when to use custom session handlers and how sessions impact web application state management.

Let’s look at another data storage mechanism—user sessions. They’re more stable than cache, but they have short-term persistence too.

What are user sessions?

Sessions are a data store for short-term user-specific data. For example, imagine Alice opens a website and logs in. The information that stores the fact she has logged in as a specific user needs to be preserved across multiple requests, so she stays logged in after refreshing the page. This information is short-term—if she opens the same website the next day, she’ll have to log in again. And, this information is user-specific, so if Bob opens the same website on another computer, he won’t be logged in as Alice. Take a second to admire how great that is.

How PHP stores sessions

It uses a key-value store with long semi-random keys, one for each user. The key is called session ID, and the value is session data. When a user starts a new session, the server generates a random 26-character (char) session ID and sets a PHPSESSID cookie containing the newly generated ...