views:

614

answers:

2

This is very simple. I write

$auth->getStorage()->write($user);

And then I want, in a separate process to load this $user, but I can't because

$user = $auth->getIdentity();

is empty. Didn't I just... SET it? Why does it not work? Halp?

A: 

Hello,

It's supposed to work.

Here's the implementation of the Auth getIdentity function.

/**
 * Returns the identity from storage or null if no identity is available
 *
 * @return mixed|null
 */
public function getIdentity()
{
    $storage = $this->getStorage();

    if ($storage->isEmpty()) {
        return null;
    }

    return $storage->read();
}

Here's the implementation of the PHP Session Storage write and read functions:

/**
 * Defined by Zend_Auth_Storage_Interface
 *
 * @return mixed
 */
public function read()
{
    return $this->_session->{$this->_member};
}

/**
 * Defined by Zend_Auth_Storage_Interface
 *
 * @param  mixed $contents
 * @return void
 */
public function write($contents)
{
    $this->_session->{$this->_member} = $contents;
}

Are you sure you are loading the same instance of the Zend_Auth class?

Are you using

$auth = Zend_Auth::getInstance();

Maybe you are calling the write method after the getIdentity method?

Anyway, as I said before, what you are doing should work.

Gastón
Well, I think I have done all kind of experiments with this thing and all I got was mental bruises. Basically, I've done it by the book, then with modifications, then with rewrites, then with abstract random experiments without reaching any coherent conclusion. When updating the storage, it worked, but only if the user landed on the same page, and would finally be usable next refresh. If I had a redirect, it wouldn't work unless it was in a class that loaded another class, that updated the storage etc etc.My conclusion is that this crap is absolutely random. Thanks though.
John
Just to confirm setting the storage directly does work for me. I use that method to force the identity in debug mode when disconnected so I can log in with my open ID login page. I think this being a session issue as described is your mostly likely problem.
Jai
A: 

So on a page reload you can fetch the session, while not if redirecting? Are you redirecting on a different Domain-Name? Then it might be an issues with the Cookies and you'd need to manually set session.cookie_domain ini variable.

Check if the cookie named PHPSESSID has been properly been set and if it's being sent to the server on every page request? Is it constant or does it change on every request?

Also, you might want to check if the session data is being properly saved to the disk. The sessions can be found in the directory defined by the ini variable session.save_path. Is the file corresponding to your PHPSESSID there and does it contain a meaningful entry? In my case it contains

root@ip-10-226-50-144:~# less /var/lib/php5/sess_081fee38856c59a563cc320899f6021f 
foo_auth|a:1:{s:7:"storage";a:1:{s:9:"user_id";s:2:"77";}}
Silvan Mühlemann
Nono, it's the same domain. I will check out the cookie, though. By this time I am almost sure it could be pretty much anything, including the global warming, heh.
John