你可以在会话中切换PHP会话吗? [英] Can You Switch PHP Sessions In a Session?

查看:109
本文介绍了你可以在会话中切换PHP会话吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程式,我想要统一。一个是我写的,另一个是我使用的CMS。我的身份验证发生在我编码的身份,我想让CMS知道这些信息。问题是CMS使用一个会话名称,我的应用程序使用另一个。我不想让它们使用相同的,因为可能的命名空间冲突,但我仍然想得到这个信息。

I have two apps that I'm trying to unify. One was written by me and another is a CMS I am using. My authentication happens in the one I coded and I'd like my CMS to know that information. The problem is that the CMS uses one session name, and my app uses another. I don't want to make them use the same one due to possible namespace conflicts but I'd still like to get this information.

是否可以切换会话名称在请求的中间?例如,在CMS中执行这样的操作:

Is it possible to switch session names in the middle of a request? For example, doing something like this in the CMS:

//session_start already called by cms by here

$oldSession = session_name();
session_name("SESSION_NAME_OF_MY_APP");
session_start();

//get values needed
session_name($oldSession);
session_start();

这样的工作吗?我在文档或网络上找不到任何东西,如果这样的话,在session_start()被调用后会工作。提示?

Would something like this work? I can't find anything in the docs or on the web if something like this would work after session_start() has been called. Tips?

不管这个解决方案,我一直在考虑只是开发一个Web服务来获取信息,但显然只是从会话中获取它是更可取的,因为该信息已经可用。

Baring this solution, I've been considering just developing a Web Service to get the information, but obviously just getting it from the session would be preferable as that information is already available.

谢谢!

推荐答案

session_id('my1session');
session_start();
echo ini_get('session.name').'<br>';
echo '------------------------<br>';
$_SESSION['value'] = 'Hello world!';
echo session_id().'<br>';
echo $_SESSION['value'].'<br>';
session_write_close();
session_id('my2session');
session_start();
$_SESSION['value'] = 'Buy world!';
echo '------------------------<br>';
echo session_id().'<br>';
echo $_SESSION['value'].'<br>';
session_write_close();
session_id('my1session');
session_start();
echo '------------------------<br>';
echo $_SESSION['value'];

日志将如下所示:


PHPSESSID
------------------------
my1session
Hello world!
------------------------
my2session
Buy world!
------------------------
Hello world!

所以,你可以看到,会话变量保存和恢复,而改变会话。

So, as you can see, session variables saved and restored while changing session.

这篇关于你可以在会话中切换PHP会话吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆