如何为每个应用程序提供不同的会话?

For eg

App.1

session_start();

$_SESSION["user_name"] = "xyz";

$_SESSION["is_login"] = True;

'''''''''''''''''''''''''''''''''''''''''''''''

App.2

session_start();

$_SESSION["user_name"] = "abc";

$_SESSION["is_login"] = false;

How to use same session like above same session for different application at same time?

Call the session_name function passing in an identifier for your app prior to calling session_start

For example

session_name("App1");
session_start();

I would do like this:

$_SESSSION['current_app'] = 'app1';

App.1

session_start();
$_SESSION['app1']["user_name"] = "xyz";
$_SESSION['app1']["is_login"] = True;

'''''''''''''''''''''''''''''''''''''''''''''''

App.2

session_start();    
$_SESSION['app2']["user_name"] = "abc";
$_SESSION['app2']["is_login"] = False;

To retrieve the current session:

$current_app = $_SESSSION['current_app'];
$user_name = $_SESSION[$current_app]["user_name"]
$is_login = $_SESSION[$current_app]["is_login"]

You could of course use session_name() as Orangepill stated, but then you will have to consider some things: (Based on reading the comments from session_name() - manual - http://php.net/manual/en/function.session-name.php)

  • You must call session_name() before each session_start() and it wouldn't return an error otherwise.
  • It's "expensive" (time of executing script)
  • Session_name() function will have no essential effect if you set session.auto_start to true in php.ini
  • You MUST use session_name() first if you want to use session_set_cookie_params(). PHP won't give you any errors even if you don't.

Have different session names ,that would be good.

You can use different session in a single application. For that you need to set you session_cookie_path differently for different session.