Zend_Auth checking if user is logged in

iceangel89 picture iceangel89 · Sep 24, 2009 · Viewed 8.5k times · Source

I tried using:

// do login if request is posted
if (isset($_POST) && !empty($_POST)) {
    // do authencation
    ...
} else {
    // request not posted
    // see if already logged in
    if (Zend_Auth::getInstance()->hasIdentity()) {
        echo "already logged in as: " . Zend_Auth::getInstance()->getIdentity();
    }
}

Zend_Auth::getInstance()->hasIdentity() seem to be always false ... even after a login

Answer

gnarf picture gnarf · Sep 24, 2009

Are you ever calling Zend_Auth::getInstance()->getStorage()->write($identity)?

If you are authenticating through Zend_Auth::getInstance()->authenticate($adapter) it will write to storage for you, but if you are calling authenticate directly on the adapter, you are responsible for writing the identity to the storage.