nah*_*hri 5 session cakephp autologin
我正在研究新项目的用户管理组件.计划是:
但是自动登录存在一些问题.这是我使用的代码的一部分:
<?php
...
// set userstatus to "active" and delete meta information "activation_key"
// then automatically login
$this->User->id = $id;
$this->User->saveField('modified', date('Y-m-d H:i:s') );
$this->User->saveField('status', 1 );
// $this->User->deleteActivationKey ....
$this->Auth->login($this->User->read());
$this->Session->setFlash(__('Successfully activated account. You are now logged in.'));
$this->User->saveField('last_login', date('Y-m-d H:i:s') );
$this->redirect(array('controller' => 'pages'));
...
Run Code Online (Sandbox Code Playgroud)
这个工作到目前为止,直到您想要使用Auth组件的user()函数获取有关登录用户的信息.
我们在AppController-> beforeRender中使用它,以便在用户信息应用程序范围内:
$this->set('auth', $this->Auth->user());
Run Code Online (Sandbox Code Playgroud)
但在自动登录操作之后,我得到了未定义的索引通知.(例如,通过在视图中访问$ auth ['id']).print_r()仅显示当前用户的用户名和哈希密码.如果您手动登录,一切正常.它必须是帐户激活后自动登录的东西.
似乎是会话的问题?我究竟做错了什么?
nah*_*hri 11
在测试了许多变体后找到了解决方案
现在使用:
$user = $this->User->findById($id);
$user = $user['User'];
$this->Auth->login($user);
Run Code Online (Sandbox Code Playgroud)
不知道为什么,我以为我已经尝试过这种方式,但是没有用.