Zend_Auth getidentity 始终返回 null

发布于 2024-11-03 02:06:28 字数 1348 浏览 1 评论 0原文

专家们,

我刚刚发现 Zend_Auth 的一个奇怪行为,即它无法在服务器中存储会话。对于我所有使用 Zend_Auth 进行身份验证的现有应用程序来说,这种情况突然发生,所以我确信这不是代码问题。基本上,每当用户成功通过身份验证(他的用户对象存储到会话中)并且重定向到登陆页面后,​​用户对象始终为 NULL。

我使用 Zend_Auth::getInstance()->getIdentity() 从会话中检索用户对象,它始终为 NULL。这种奇怪的行为只发生在实时服务器中,并且在我的计算机和登台服务器中一切正常。我只是想确保这只是服务器试图在这里搞笑,因为我一直在检查代码,但仍然一无所知。这是一个共享服务器,我没有太多访问权限。

这是我的代码:


// setup Zend_Auth adapter for a database table
Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'Users', 'Email', 'Password', 'MD5(?) AND Active=1');
$authAdapter->setIdentity($email)
             ->setCredential($password);

// do the authentication
$auth = Zend_Auth::getInstance();
$result = $authAdapter->authenticate();

if ($result->isValid()) {
     // success : store database row to auth's storage system
     // (not the password though!)
     $userData = array('UID','Email','Username','FirstName','LastName','Email','School');
     $data = $authAdapter->getResultRowObject($userData, 'Password');
     $auth->getStorage()->write($data);

     $userData = get_object_vars($auth->getIdentity());
     if (!empty($userData)) {
         // redirect here
     } else {
         // show invalid
     }
} else {
         // show invalid
}

Experts,

I just found a weird behavior of Zend_Auth whereby it's not able to store session in the server. This happens just suddenly to all of my existing applications that use Zend_Auth for authentication purpose, so I'm sure it's not a problem with the codes. Basically, whenever the user is successfully authenticated (his user object is stored into the session) and after redirect to the landing page, the user object is always NULL.

I use Zend_Auth::getInstance()->getIdentity() to retrieve the user object from the session and it's always NULL. This weird behavior only happens in the live server and everything works just fine in my machine and staging server. I just want to make sure that it's just the server trying to be funny here coz I've been checking around the codes and still remain clueless. It's a shared server and I don't have much access.

Here is my code:


// setup Zend_Auth adapter for a database table
Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'Users', 'Email', 'Password', 'MD5(?) AND Active=1');
$authAdapter->setIdentity($email)
             ->setCredential($password);

// do the authentication
$auth = Zend_Auth::getInstance();
$result = $authAdapter->authenticate();

if ($result->isValid()) {
     // success : store database row to auth's storage system
     // (not the password though!)
     $userData = array('UID','Email','Username','FirstName','LastName','Email','School');
     $data = $authAdapter->getResultRowObject($userData, 'Password');
     $auth->getStorage()->write($data);

     $userData = get_object_vars($auth->getIdentity());
     if (!empty($userData)) {
         // redirect here
     } else {
         // show invalid
     }
} else {
         // show invalid
}

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

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

发布评论

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

评论(1

最美不过初阳 2024-11-10 02:06:28

听起来您服务器上的 /tmp 文件夹已满,这意味着如果您使用基于文件的会话,该会话将始终为空。看看能不能确认一下。

此代码片段将告诉您计算机中磁盘的容量:

echo `df -h`."\n";

此代码片段将确认您是否正在使用文件以及它们的存储位置。

echo ini_get('session.save_handler')."\n";
echo ini_get('session.save_path')."\n";

如果第二个 ini_get 没有值,那么它将默认为 /tmp

如果保存路径位于您的主文件夹中,那么您可以自己清除它,但如果保存路径位于 /tmp 或其他系统文件夹中,您需要联系您的托管提供商。

It sounds like the /tmp folder on your server is full meaning that if you're using file-based sessions, the session will always be empty. See if you can confirm it.

This snippet will tell you how full the disks are in the machine:

echo `df -h`."\n";

This snippet will confirm if you're using files and where they are stored.

echo ini_get('session.save_handler')."\n";
echo ini_get('session.save_path')."\n";

If the second ini_get has 'no value', then it'll default to /tmp

If the save path is within your home folder, then you can clear that out yourself, but if the save path is in /tmp or some other system folder, you'll need to get onto your hosting provider.

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