跨多个请求使用Zend会话命名空间 [英] Working with Zend Session Namespaces across multiple requests
问题描述
也许我只是没有看到它,但是除了直接调用$_SESSION
之外,还有其他任何方法可以访问以前创建的会话名称空间吗?除了我真的不想这样做的事实外,Zend文档还
Maybe I'm just not seeing it, but is there any other way to access a previously created session namespace, other than calling $_SESSION
directly? Besides the fact that I really don't want to do this, the Zend documentation also advises against this:
$ _ SESSION仍在 PHP的全局名称空间,开发人员 应该避免直接访问 它,以便Zend_Session和 Zend_Session_Namespace可以最多 有效,安全地提供其 与会话相关的套件 功能.
while $_SESSION is still available in PHP's global namespace, developers should refrain from directly accessing it, so that Zend_Session and Zend_Session_Namespace can most effectively and securely provide its suite of session related functionality.
Zend_Session_Namespace类没有获取名称空间的静态方法,尽管现在已弃用 Zend_Session_Namespace#getIterator
,该方法不是静态的.
The Zend_Session_Namespace class doesn't have a static method for getting a namespace, and although the now deprecated namespaceGet
method in Zend_Session instructs me to use Zend_Session_Namespace#getIterator
, that method is not static.
所以这意味着我需要使用new
关键字初始化一个新的名称空间.问题是,这不包括先前设置的变量:
So that means I need to initialize a new namespace, using the new
keyword. The problem is, this doesn't include previously set variables:
$ns = new Zend_Session_Namespace('foo');
$ns->foo = 'bar';
在随后的请求中,此:
print_R(new Zend_Session_Namespace('Foo'));
...打印此:
Zend_Session_Namespace Object
(
[_namespace:protected] => Foo
)
这似乎很明显.
那么我应该如何在不使用$_SESSION['Foo']
的情况下获取先前创建的名称空间?
So how am I supposed to fetch the previously created namespace, without using $_SESSION['Foo']
?
推荐答案
您的两个代码示例的大小写不匹配(foo vs. Foo),我不确定这是否只是一个错字. Zend_Session_Namespace只是$ _SESSION的包装,因此您需要做的就是使用相同的键创建一个名称空间对象,然后所有数据都应该可用.
The case of your two code examples doesn't match (foo vs. Foo), I'm not sure if that was just a typo or not. Zend_Session_Namespace is just a wrapper for $_SESSION, so all you need to do is create a namespace object with the same key and then all your data should be available.
$ns = new Zend_Session_Namespace('foo');
$ns->foo = 'bar';
,然后在另一页上:
$ns = new Zend_Session_Namespace('foo');
echo $ns->foo; // should output bar
如果这不起作用,则说明您的会话配置有问题.
if this doesn't work then there is a problem with your session configuration.
这篇关于跨多个请求使用Zend会话命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!