Reputation: 91
I have 2 separate PHP apps running on the same domain server: abc.com/demo & abc.com/live
There is session_start() on almost every PHP page in both these apps. As soon as a single user interacts with the second app, the first app freezes or stops working. I think the same session ID is being used by both, which causes this conflict.
I read on PHP manual that specifying the PHP session name like
session_name("DEMO");
or session_name("LIVE"); may help.
My question is, do I need to specify the session_name on each and every PHP page in both the apps, or is only required when the session is first created, during the login success process.
Kindly advise. Thanks.
Upvotes: 3
Views: 205
Reputation: 1672
If multiple independent web pages want to use sessions, just assign a subarray to each one inside $_SESSION. For example, $_SESSION['one']=[];
and $_SESSION['two']=[];
. You can update or clear each separate subarray independently. Always use one of these subarrays and there will be no interference. For example, $_SESSION['one']['logged_in']=true;
.
Upvotes: 0
Reputation: 1192
If you mean that your number of PHP files and you want to access some value that is stored in the PHP session then you should session_start(); for every file
You can separate your session like this
session_name($isAPP_ONE ? "APP_ONE" : "APP_TWO");
like store_session.php
session_start();
$_SESSSION["username"] = "Md Zahidul Islam";
home.php
session_start();
$_SESSSION["username"]; // this will give you Md Zahidul Islam
home-two.php
session_start();
$_SESSSION["username"]; // this will give you Md Zahidul Islam
And if you use work like this, then there is no need to use session_start(); on every file.
like header.php
session_start();
home.php
include("header.php");
$_SESSSION["username"]; // this will return you Md Zahidul Islam and this value will come from the store_session.php file
home-two.php
include("header.php");
$_SESSSION["username"]; // this will return you Md Zahidul Islam and this value will come from the store_session.php file
Upvotes: 1
Reputation: 8796
"On each and every ... page" is the right way.
But separate it into another script, and include that in other scripts.
Because you name your sessions "DEMO" and "LIVE", maybe place logic to decide session in a file which is shared and/or used by both apps.
$isProduction = ...;
session_name($isProduction ? "LIVE" : "DEMO");
session_start();
Upvotes: 2