多个 PHP 会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/854105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple PHP Sessions
提问by Nathan H
I am to build a PHP application for a website that already has another PHP application running on the same domain/server. My app will of course be using sessions, and I don't want my sessions to interfere with the existing app. For example if I want to use $_SESSION['username'], maybe the other app also uses $_SESSION['username'], which could be a problem. I'm not looking for an extra layer of security, I trust the application I'm sharing the host with. I just want to avoid bugs.
我要为一个网站构建一个 PHP 应用程序,该网站已经在同一域/服务器上运行了另一个 PHP 应用程序。我的应用程序当然会使用会话,我不希望我的会话干扰现有的应用程序。例如,如果我想使用 $_SESSION['username'],也许其他应用程序也使用 $_SESSION['username'],这可能是一个问题。我不是在寻找额外的安全层,我相信与我共享主机的应用程序。我只是想避免错误。
One way would be to do something like $_SESSION['MY_APP_NAME']['username'], but I want to know if there is an easier way.
一种方法是执行类似 $_SESSION['MY_APP_NAME']['username'] 的操作,但我想知道是否有更简单的方法。
I see on the PHP documentation that there is a function called 'session_module_name'. The name sounds good, but the docs don't really explain what it is for.
我在 PHP 文档中看到有一个名为“session_module_name”的函数。这个名字听起来不错,但文档并没有真正解释它的用途。
Any advice?
有什么建议吗?
回答by Powerlord
There is an easier way: session_name.
有一种更简单的方法:session_name。
Prior to calling session_start();call session_name("something");(where you change something to whatever you want it to be called).
在调用session_start();call之前session_name("something");(您将某些内容更改为您想要调用的任何内容)。
回答by djn
Another thing that may help you in keeping apps separate is move the session storage to another place either setting session.save_pathin php.ini to a folder of your choice or calling session_save_path()before session_start().
另一件可以帮助您将应用程序分开的事情是将会话存储移动到另一个地方,要么session.save_path在 php.ini 中设置到您选择的文件夹,要么session_save_path()在 session_start() 之前调用。

