Zend_Auth 存储如何工作?
这很简单。我写
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该可以工作。
这是 Auth getIdentity 函数的实现。
下面是 PHP 会话存储写入和读取函数的实现:
您确定正在加载 Zend_Auth 类的同一个实例吗?
您是否正在使用
也许您在 getIdentity 方法之后调用 write 方法?
不管怎样,正如我之前所说,你所做的应该有效。
It's supposed to work.
Here's the implementation of the Auth getIdentity function.
Here's the implementation of the PHP Session Storage write and read functions:
Are you sure you are loading the same instance of the Zend_Auth class?
Are you using
Maybe you are calling the write method after the getIdentity method?
Anyway, as I said before, what you are doing should work.
因此,在页面重新加载时,您可以获取会话,而如果重定向则不能获取会话?您是否重定向到不同的域名?那么可能是 Cookie 出现问题,您需要手动设置
session.cookie_domain
ini 变量。检查名为
PHPSESSID
的 cookie 是否已正确设置以及是否在每个页面请求中将其发送到服务器?它是恒定的还是会根据每个请求而变化?另外,您可能想检查会话数据是否正确保存到磁盘。可以在 ini 变量
session.save_path
定义的目录中找到会话。该文件是否与您的PHPSESSID
相对应,并且它是否包含有意义的条目?就我而言,它包含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 yourPHPSESSID
there and does it contain a meaningful entry? In my case it contains添加:
到index.php之前:
Add:
to index.php before: