【发布时间】:2015-01-28 06:32:28
【问题描述】:
上周末,我试图解决一个网站上的错误,该网站上的 Session 没有保存在 IE 中 - 今天我用笔记本电脑在该网站上做进一步的工作,但我无法再登录 - 我总是有做了一件非常愚蠢的事情。
我在 Windows 笔记本电脑上使用 xampp,并在 localhost 上工作,这发生在所有浏览器中。我对解决这类问题不是很有经验 - 我已经确定了以下几点:
- 用户能够登录(Auth->login() 成功登录用户),问题是重定向时会话消失了
- 我可以看到在我的 /tmp/ 目录中写入的 Sessions 包含(看起来是)正确的数据
- 我可以创建自己的愚蠢 cookie,并且它们的值仍然存在
- 该网站不存在其他 cookie
所以,在我看来,会话 cookie 没有被设置,但我已经没有关于为什么会发生这种情况的想法。我没有更改任何与 cookie 相关的浏览器设置(在 IE 中启用 cookie 之外),并且我已经仔细检查了我的 Chrome cookie 设置。正如我所提到的,我还在 AppController 中编写了一些垃圾 cookie,我可以看到它们被创建,并且它们的数据仍然存在。
如果我在 login() 之后调用 $_SESSION,一切看起来都很好,但如果我在登录之前打印 $_SESSION,它是空的。
我很确定我已经设法做一些迟钝的事情,但我已经没有关于它可能是什么的想法了。我已将 /app/core.php 恢复为 Cake 默认值:
Configure::write('Session', array(
'defaults' => 'php'
));
我的 login() 函数基本上如下所示:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again.'));
}
AppController 中的身份验证设置:
class AppController extends Controller {
public $components = array(
'Session',
'Cookie',
'Acl',
'Email',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'password')
)),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
),
);
以及在 login() 中重定向之前打印 $this->Auth->user(), $_SESSION 的示例输出:
\app\Controller\UsersController.php (line 203)
array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super@test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
\app\Controller\UsersController.php (line 204)
array(
'Config' => array(
'userAgent' => '4af162a3a94462226b6e93c6806203aa',
'time' => (int) 1417317929,
'countdown' => (int) 10,
'language' => 'eng'
),
'Auth' => array(
'User' => array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super@test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
)
)
上次创建的会话文件:
配置|a:4:{s:9:"userAgent";s:32:"4af162a3a94462226b6e93c6806203aa";s:4:"时间";i:1417317929;s:9:"倒计时";i:10; s:8:"语言";s:3:"eng";}Auth|a:1:{s:4:"用户";a:12:{s:2:"id";s:2:" 10";s:4:"姓名";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"super@ test.com";s:8:"group_id";s:1:"3";s:7:"地址";s:1:"3";s:5:"电话";s:10:" xxxxx";s:8:"类别";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"已创建";s:19:"2014- 11-29 16:27:19";s:8:"修改";s:19:"2014-11-29 16:27:19";s:5:"组";a:4:{s: 2:“id”;s:1:“3”;s:4:“名称”;s:14:“管理员”;s:7:“创建”;s:19:“2014-11-16 21: 01:35";s:8:"修改";s:19:"2014-11-16 21:01:35";}}}
【问题讨论】:
标签: php session cakephp authentication cookies