2

Having problems accessing session variables through different actions in ZF.

Here are the contents of the bootstrapper:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initSession()
    {
            Zend_Session::start();
            $sessionUser = new Zend_Session_Namespace('sessionUser');
    }

}

I assign a variable to $sessionUser in the IndexController:

                    $sessionUser->userId = $res->userId;
                    $sessionUser->organisationId = $res->organisation_organisationId;
                    $sessionUser->forename = $res->forename;

And attempt to access the variable in the Administrator controller:

$sessionUser->organisationId

Yet I receive the error:

Notice: Undefined variable: sessionUser in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17 Notice: Trying to get property of non-object in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17

And ideas what could be causing this?

Many thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
kaese
  • 10,249
  • 8
  • 29
  • 35

2 Answers2

4

To get the session variable back in your controller you also need to do:

$sessionUser = new Zend_Session_Namespace('sessionUser');
Marcin
  • 215,873
  • 14
  • 235
  • 294
4

Well, the error you are getting is obvious. The $sessionUser is not defined.

You must initialize such variable before assinging values to it. Put this in your controller:

 $sessionUser = new Zend_Session_Namespace('sessionUser');
Richard Knop
  • 81,041
  • 149
  • 392
  • 552