Home > OS >  How to separate 2 PHP apps running on same server from using a common session
How to separate 2 PHP apps running on same server from using a common session

Time:12-28

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.

CodePudding user response:

"On each and every ... page" is the right way.

But separate it into another script, and include that in other scripts.

Example

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();

CodePudding user response:

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

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
  • Related