PHP Sessions - Beginners Level

Before you learn about sessions in PHP, it is needed to understand what are HTTP Sessions.

HTTP Session is a mechanism that allows your software to store information (on the server side) about the user, simulating a state across page navigation. It’s different from cookies, which store the information on the user’s computer.

Remember, HTTP Protocol is stateless

Session Management in PHP

Session management in PHP is relatively straightforward and is commonly used to track user information across multiple pages. Here’s an overview of the steps typically used:

  1. Start a Session: You initiate a session by calling the session_start() function.
  2. Storing Session Variables: After starting a session, you can store information within the $_SESSION superglobal array.
  3. Accessing Session Variables: You can access these variables on other pages by again starting the session
  4. Altering Session Variables: You can change the value of session variables by simply overwriting them.
  5. Destroying a Session: If you want to remove some session data, you can unset individual variables using unset()
  6. Security Considerations: You should also consider using secure sessions, especially if handling sensitive information.

Read the posts in this serie to learn how to use sessions on PHP.

What is a PHP Session

As seen in What is HTTP Sessions, a session ia a way to persist user data (or software needs data) across several requests from the same Http client.

In PHP, a SESSION is a global array used to store information about a user’s individual session. It’s a way that enables you to create personalized...