也许我只是没有看到它,但是除了调用 $_SESSION
之外,还有其他方法可以访问以前创建的 session 命名空间吗?直接地?除了我真的不想这样做,Zend 文档也advises against this :
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 类没有用于获取命名空间的静态方法,尽管现在已弃用
namespaceGet
Zend_Session 中的方法指示我使用 Zend_Session_Namespace#getIterator
,该方法不是静态的。所以这意味着我需要初始化一个新的命名空间,使用
new
关键词。问题是,这不包括先前设置的变量:$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']
的情况下获取之前创建的命名空间? ?
最佳答案
你的两个代码示例的情况不匹配(foo vs. Foo),我不确定这是否只是一个错字。 Zend_Session_Namespace 只是 $_SESSION 的包装器,所以您需要做的就是创建一个具有相同键的 namespace 对象,然后您的所有数据都应该可用。
$ns = new Zend_Session_Namespace('foo');
$ns->foo = 'bar';
然后在另一个页面上:
$ns = new Zend_Session_Namespace('foo');
echo $ns->foo; // should output bar
如果这不起作用,则说明您的 session 配置有问题。
关于php - 跨多个请求使用 Zend session 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6623599/