John
John

Reputation: 261

How does Zend_Auth storage work?

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;
    }

}

?>

Upvotes: 5

Views: 10522

Answers (3)

Piotr Szymaniak
Piotr Szymaniak

Reputation: 1

Add:

register_shutdown_function('session_write_close');

to index.php before:

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

Upvotes: 0

Silvan M&#252;hlemann
Silvan M&#252;hlemann

Reputation: 580

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";}}

Upvotes: 0

Gaston
Gaston

Reputation: 1848

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.

Upvotes: 1

Related Questions