开发者

Working with Zend Session Namespaces across multiple requests

开发者 https://www.devze.com 2023-03-19 07:30 出处:网络
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

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开发者_StackOverflow seems obvious.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消