Ryan
Ryan

Reputation: 6057

How to handle PHP session variable names for two instances of application running on same server?

For example let's say I have an app that uses $_SESSION['user_id']. Now if I have two of these applications running on the same server then they will be sharing this user_id variable which would break things.

The only thing I can think of is to prepend some unique id like this:

$_SESSION['/app1/user_id']

$_SESSION['/app2/user_id']

is that the best option?

Upvotes: 3

Views: 667

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270677

This is the purpose of session_name(). Assign a different name to each application's session to avoid collisions between $_SESSION keys. The name will be used as the session cookie's name so although both session cookies will be passed to both applications, only the one matching the application's session_name() will be used to populate $_SESSION.

// App 1
session_name('app1');
session_start();

// App 2
session_name('app2');
session_start();

Upvotes: 6

Related Questions