5

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?

[EDIT 2011-04-13]

This has been asked almost two years ago. Fact is, though, that I repeated the question in July 2010 and got a fantastic answer that I back then simply did not understand.

Link: Zend_Auth fails to write to storage

I have since built a very nice litte class that I use (sometimes with extra tweaking) in all my projects using the same storage engine as Zend_Auth but circumventing all the bad.

<?php

class Qapacity_Helpers_Storage {

    public function save($name = 'default', $data) {

        $session = new Zend_Session_Namespace($name);
        $session->data = $data;

        return true;
    }

    public function load($name = 'default', $part = null) {

        $session = new Zend_Session_Namespace($name);

        if (!isset($session->data))
            return null;

        $data = $session->data;

        if ($part && isset($data[$part]))
            return $data[$part];

        return $data;
    }

    public function clear($name = 'default') {

        $session = new Zend_Session_Namespace($name);

        if (isset($session->data))
            unset($session->data);

        return true;
    }

}

?>
Community
  • 1
  • 1
John
  • 261
  • 1
  • 3
  • 16
  • The other problem might be that the storage (and Zend_Auth) doesn't have the same configuration. Writing on one place, reading on another one. – AsTeR Feb 14 '12 at 20:26

3 Answers3

1

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.

Gaston
  • 1,828
  • 2
  • 15
  • 29
  • 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 Oct 20 '09 at 10:28
  • 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 Mar 24 '10 at 00:26
0

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";}}
  • 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 Oct 22 '09 at 10:21
0

Add:

register_shutdown_function('session_write_close');

to index.php before:

$application->bootstrap()->run();