Home > DeveloperSection > Forums > Handle php session variable for two instance running on same server
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    November-01-2014 12:20 AM

 PHP Session  Session Variable  Session Management 
Ratings:


 1 Reply(s)
 588  View(s)
Rate this:

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['/application1/user_id']

$_SESSION['/application2/user_id']



Pravesh Singh

Total Post:411

Points:2881
Posted on    November-01-2014 12:21 AM

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.

session_name('application1');
session_start();

session_name('application2');
session_start();

Don't want to miss updates? Please click the below button!

Follow MindStick