Zend_Auth 存储如何工作?

发布于 2024-08-04 13:37:26 字数 1306 浏览 5 评论 0原文

这很简单。我写

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

然后我想在一个单独的进程中加载​​这个 $user,但我不能,因为

$user = $auth->getIdentity();

它是空的。我不是刚刚……设置了吗?为什么它不起作用?哈尔普?

[编辑2011-04-13]

大约两年前就有人问过这个问题。但事实是,我在 2010 年 7 月重复了这个问题,并得到了一个我当时根本无法理解的精彩答案。

链接: Zend_Auth 无法写入存储

我已经构建了一个非常我在所有项目中使用的漂亮的小类(有时需要额外的调整),使用与 Zend_Auth 相同的存储引擎,但避免了所有的坏处。

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

}

?>

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

}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感悟人生的甜 2024-08-11 13:37:26

它应该可以工作。

这是 Auth getIdentity 函数的实现。

/**
 * 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();
}

下面是 PHP 会话存储写入和读取函数的实现:

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

您确定正在加载 Zend_Auth 类的同一个实例吗?

您是否正在使用

$auth = Zend_Auth::getInstance();

也许您在 getIdentity 方法之后调用 write 方法?

不管怎样,正如我之前所说,你所做的应该有效。

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.

雪化雨蝶 2024-08-11 13:37:26

因此,在页面重新加载时,您可以获取会话,而如果重定向则不能获取会话?您是否重定向到不同的域名?那么可能是 Cookie 出现问题,您需要手动设置 session.cookie_domain ini 变量。

检查名为 PHPSESSID 的 cookie 是否已正确设置以及是否在每个页面请求中将其发送到服务器?它是恒定的还是会根据每个请求而变化?

另外,您可能想检查会话数据是否正确保存到磁盘。可以在 ini 变量 session.save_path 定义的目录中找到会话。该文件是否与您的 PHPSESSID 相对应,并且它是否包含有意义的条目?就我而言,它包含

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

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";}}
狼性发作 2024-08-11 13:37:26

添加:

register_shutdown_function('session_write_close');

到index.php之前:

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

Add:

register_shutdown_function('session_write_close');

to index.php before:

$application->bootstrap()->run();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文