user12067856
user12067856

Reputation:

$ _SESSION and more users - PHP

A very trivial question, but it is a thought that came to me and I don't know if it can be pertinent or not, if for example in the login page, or any other page, we initialize the $_SESSION ['name_session']; and in the logout phase we are going to destroy them, what happens if several users simultaneously use a web portal.

I explain better that we have two users: user1: enter the portal and the $_SESSION begins

Meanwhile

User2: he also connects

if user1 closes the $_SESSION, could it happen that even user2 will log out?

If, yes, you start the $_SESSION, with the user id it might be a good thing, so would the $_SESSIONs all have unique keys?

Upvotes: 1

Views: 44

Answers (3)

Giso Stallenberg
Giso Stallenberg

Reputation: 979

The session is not shared. Each user (browser / client) has it's own session. A cookie is used to track the individual sessions, as Dharman said. Anything you store in $_SESSION is stored for that individual user and is retrieved again using the session id from the cookie in the next request of that client.

Upvotes: 1

Barmar
Barmar

Reputation: 781493

PHP sessions are connected to a specific browser session. Each client user gets their own session, and changes made to one session have no effect on other clients.

This is done using a cookie that's sent to the browser. When you start a session, it creates a random session ID, and this is set as the PHPSESSID cookie. When the browser sends back this cookie, it allows PHP to find the corresponding session data.

Upvotes: 2

Ezequiel Esnaola
Ezequiel Esnaola

Reputation: 60

By default, it is saved in session cache (OPcache) and it is not necessary to add the user's id, php takes care of that.

Upvotes: -2

Related Questions