[英]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']
. 例如,假设我有一个使用
$_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. 现在,如果我有两个这样的应用程序在同一台服务器上运行,那么他们将共享这个user_id变量,这会破坏事情。
The only thing I can think of is to prepend some unique id like this: 我唯一能想到的是预先添加一些像这样的唯一ID:
$_SESSION['/app1/user_id']
$_SESSION['/app2/user_id']
is that the best option? 那是最好的选择吗?
This is the purpose of session_name()
. 这是
session_name()
的目的。 Assign a different name to each application's session to avoid collisions between $_SESSION
keys. 为每个应用程序的会话分配一个不同的名称,以避免
$_SESSION
键之间发生冲突。 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
. 该名称将用作会话cookie的名称,因此虽然两个会话cookie都将传递给两个应用程序,但只有与应用程序的
session_name()
匹配的cookie才会用于填充$_SESSION
。
// App 1
session_name('app1');
session_start();
// App 2
session_name('app2');
session_start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.