简体   繁体   English

如何处理在同一服务器上运行的两个应用程序实例的PHP会话变量名?

[英]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.

相关问题 PHP如何处理1个文件上具有相同变量名的同时命中 - How does PHP handle simultaneous hits with same variable names on 1 file 同一台服务器上的两个应用程序存在会话问题? - Session issue with two application on same server? 如何将两个域名绑定到在同一Azure Ubuntu VM中运行的两个不同的网站(php和nodejs)? - How to bind two domain names to two different websites(php and nodejs) running in same Azure Ubuntu VM? 在同一服务器上运行两个PHP版本(STRETCH) - Running two PHP versions on the same server (STRETCH) 在同一台服务器上运行两个 PHP 版本 - Running two PHP versions on the same server 如何在同一服务器上的Web应用程序(Core PHP)和另一个Web应用程序(Laravel)之间共享会话? - How to share Session between Web Application (Core PHP) and another Web Application (Laravel) on Same Server? 在同一服务器上的两个不同文件夹中使用相同的PHP会话 - Use the same PHP session on two different folders on the same server 循环PHP的动态$ _SESSION变量会话名称 - Dynamic $_SESSION variable session names for loop PHP 如何将在同一台服务器上运行的 2 个 PHP 应用程序与使用公共 session 的应用程序分开 - How to separate 2 PHP apps running on same server from using a common session 两个域,相同的数据。 PHP应用程序仅在其中之一上丢失会话变量,怎么办? - Two domains, same data. PHP Application loses Session vars on only one of them, how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM