CakePHP 2.0帐户激活后自动登录
题
我只是在研究我们新项目的用户管理组件。 该计划是:
- 用户在页面上注册,帐户数据数量最小(用户名,通过,电子邮件)
- 用户通过激活链接获取电子邮件以激活帐户
- 用户点击链接并激活他的帐户
- 系统在激活后自动登录用户,并将其重定向到具有帐户信息的仪表板(最后一次登录,Hi“用户名”等)。
但自动登录有一些问题。这是我使用的代码的一部分:
.<?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')); ...
到目前为止,在您希望使用Auth组件的用户()函数中获取有关登录用户的信息,直到您想要获取有关登录用户的信息。
我们在AppController-> Beforerender中使用它,以具有用户信息宽:
.$this->set('auth', $this->Auth->user());
但在该自动登录操作之后,我得到了未定义的索引通知。 (例如,通过访问视图中的$ auth ['ID])。 print_r()仅显示当前用户的用户名和哈希密码。 如果您手动登录,一切正常。在帐户激活后,它必须是自动登录的东西。
似乎是会话的问题?我做错了什么?
解决方案
在测试许多变化后发现了一个解决方案。
现在用:
$user = $this->User->findById($id);
$user = $user['User'];
$this->Auth->login($user);
.
不知道为什么,我以为我已经尝试了,这没有工作。
其他提示
你试过这个吗?(Cakephp 2.x)
public function signup() {
if (!empty($this->request->data)) {
// Registration stuff
// Auto login
if ($this->Auth->login()) {
$this->redirect('/');
}
}
}
.
简单!
不隶属于 StackOverflow