Working with Zend Session Namespaces across multiple requests

我只是一个虾纸丫 提交于 2019-12-11 10:04:12

问题


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:

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.

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.

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';

On a subsequent request, this:

print_R(new Zend_Session_Namespace('Foo'));

...prints this:

Zend_Session_Namespace Object
(
    [_namespace:protected] => Foo
)    

which seems obvious.

So how am I supposed to fetch the previously created namespace, without using $_SESSION['Foo']?


回答1:


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';

and then on another page:

$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.



来源:https://stackoverflow.com/questions/6623599/working-with-zend-session-namespaces-across-multiple-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!