Jedediah
Jedediah

Reputation: 1944

Zend session seems to be restarted on each page load

I've been having a lot of trouble trying to get Zend sessions to work properly. Basically, what I need to do is set a variable once when the site loads. This variable should then remain constant throughout the lifetime of the session.

To help make this a little more clear, let's take the following scenario:

In Bootstap.php, I have

protected function _initSession() {
    $session = new Zend_Session_Namespace();
    $session->testValue = rand(0, 100);
}

Then, I access it elsewhere in the application as follows:

$session = new Zend_Session_Namespace();
echo $session->testValue;

Now what I'd hope is that $session->testValue would be assigned once, and that value would be the same on every subsequent page request. But in reality, what's happening is that $session->testValue is a new random number each time.

What am I doing wrong?

Upvotes: 0

Views: 259

Answers (1)

nachito
nachito

Reputation: 7035

Every time Bootstrap.php is run it's overwriting the testValue property. Only set it if it hasn't been set yet.

protected function _initSession() {
    $session = new Zend_Session_Namespace();
    if (!isset($session->testValue)) {
        $session->testValue = rand(0, 100);
    }
}

Upvotes: 2

Related Questions