简体   繁体   English

使用自定义会话处理程序时,如何从PHP的defualt会话中获取值?

[英]How do I get a value from PHP's defualt session when a custom session handler is in use?

I have a non CMS page in which a session value is stored using PHP's default session handler. 我有一个非CMS页面,其中使用PHP的默认会话处理程序存储会话值。 ie: 即:

session_start();
$_SESSION['MyVar'] = true;

On another page, which is part of the CMS, I need to test whether the variable is true or not. 在CMS的另一页上,我需要测试变量是否为true。 But, the CMS uses it's own session handler, so when I try to read the variable, it's undefined, because it's looking for it in the CMS's session storage system, not PHP's default. 但是,CMS使用它自己的会话处理程序,因此当我尝试读取该变量时,它是未定义的,因为它是在CMS的会话存储系统中寻找它的,而不是PHP的默认值。

How can I switch over for a moment and read a value from the default session store, and then reinstate the CMS's so that it functions correctly? 如何切换一会儿并从默认会话存储中读取一个值,然后恢复CMS使其正确运行?

Something like: 就像是:

$SessionHandler = session_get_current_handler(); // save the CMS session handler
session_use_default(); // Use PHP's default handler
$Value = $_SESSION['MyVar']; // Get the value I need
session_set_handler($SessionHandler); // Restore CMS handler

At some point you will need to call session_start() in order to populate the $_SESSION superglobal array. 在某些时候,您将需要调用session_start()来填充$_SESSION超全局数组。 You 'might' be able to call session_write_close() , change the handler, and do session_start() again. 您可以“调用” session_write_close() ,更改处理程序,然后再次执行session_start()

Try it, it will look something like 试试看,它看起来像

$Sessionhandler = session_get_current_handler();
session_use_default();
session_start();
$value = $_SESSION['var'];
session_write_close();
session_set_handler($Sessionhandler);
session_start();

I almost doubt it is going to work though. 我几乎怀疑它是否会起作用。 The only reason it 'might' work is because of the session_write_close() as PHP normally throws an E_NOTICE if you try to call session_start() twice. 它“可能”工作的唯一原因是session_write_close()因为如果您尝试两次调用session_start() ,PHP通常会抛出E_NOTICE

Depending upon the implementation, it might try to get the data again. 根据实现的不同,它可能会尝试再次获取数据。 But a lot of people over at the documentation comments have seen weird things happen when calling session_start() twice, even after session_write_close() 但是很多人在文档注释中看到,即使在session_write_close()之后,两次调用session_start()也会发生奇怪的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM