如何使用CakePHP 2x中的自动登录组件自动重定向具有自动登录cookie的用户?
我正在使用带有自动登录组件的cakephp2x。问题是,我可以写这些东西,但我不知道如何实现它来读取和授权。当用户到达页面时,浏览器中仍有cookie,但如何授权 我的登录脚本:如何使用CakePHP 2x中的自动登录组件自动重定向具有自动登录cookie的用户?,php,cakephp,cakephp-2.1,remember-me,autologin,Php,Cakephp,Cakephp 2.1,Remember Me,Autologin,我正在使用带有自动登录组件的cakephp2x。问题是,我可以写这些东西,但我不知道如何实现它来读取和授权。当用户到达页面时,浏览器中仍有cookie,但如何授权 我的登录脚本: public function login() { if ($this->Auth->user('id')) { $this->redirect(array('action' => 'dashboard')); } if($
public function login() {
if ($this->Auth->user('id')) {
$this->redirect(array('action' => 'dashboard'));
}
if($this->request->data['User']['auto_login']):
$this->AutoLogin->write($this->request->data['User']['username'],
$this->request->data['User']['password']);
endif;
if ($this->request->is('post')) {
if ($this->Auth->login( )) {
//$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
return $this->redirect($this->Auth->redirect( ));
}
else
{
$this->Session->setFlash(__('Username or Password is incorrect'), 'default', array( ), 'auth');
}
}
这应该是这样的:
public function login()
{
if ($this->request->is('post'))
{
if ($this->Auth->login())
{
if ($this->request->data['User']['persist'] == '1')
{
$cookie = array();
$cookie['username'] = $this->data['User']['USER_LOGINNAME'];
$cookie['password'] = $this->data['User']['USER_PASSWORD'];
$this->Cookie->write('Auth.User', $cookie, true, '+4 weeks');
}
$this->redirect($this->Auth->redirect());
}
else
{
$this->Session->setFlash('Your username or password was incorrect.', 'default/flash-error');
}
}
else
{
$user = $this->Auth->user();
if (empty($user))
{
$cookie = $this->Cookie->read('Auth.User');
if (!is_null($cookie))
{
$user = $this->User->find('first', array('conditions' => array('USER_LOGINNAME' => $cookie['username'], 'USER_PASSWORD' => AuthComponent::password($cookie['password']))));
if ($this->Auth->login($user['User']))
{
$this->Session->delete('Message.auth');
$this->redirect($this->Auth->redirect());
}
else
{
$this->Cookie->delete('Auth.User');
}
}
}
else
{
$this->redirect($this->Auth->redirect());
}
}
}
这使您了解了如何实现相同的任务,但是,我根据我的数据库结构使用了表单字段
请根据您的数据库结构更改表单字段。这应该类似于:
public function login()
{
if ($this->request->is('post'))
{
if ($this->Auth->login())
{
if ($this->request->data['User']['persist'] == '1')
{
$cookie = array();
$cookie['username'] = $this->data['User']['USER_LOGINNAME'];
$cookie['password'] = $this->data['User']['USER_PASSWORD'];
$this->Cookie->write('Auth.User', $cookie, true, '+4 weeks');
}
$this->redirect($this->Auth->redirect());
}
else
{
$this->Session->setFlash('Your username or password was incorrect.', 'default/flash-error');
}
}
else
{
$user = $this->Auth->user();
if (empty($user))
{
$cookie = $this->Cookie->read('Auth.User');
if (!is_null($cookie))
{
$user = $this->User->find('first', array('conditions' => array('USER_LOGINNAME' => $cookie['username'], 'USER_PASSWORD' => AuthComponent::password($cookie['password']))));
if ($this->Auth->login($user['User']))
{
$this->Session->delete('Message.auth');
$this->redirect($this->Auth->redirect());
}
else
{
$this->Cookie->delete('Auth.User');
}
}
}
else
{
$this->redirect($this->Auth->redirect());
}
}
}
这使您了解了如何实现相同的任务,但是,我根据我的数据库结构使用了表单字段
请根据您的数据库结构更改表单字段。Woah!这是一个巨大的变化。让我试试看。请耐心听我说。你的密码确实帮我找到了(+1)个地方。然而,在我结束这个问题之前的最后一个查询是,我正在使用FacebookPluginforAuth(FBConnect),它有一行名为Session\u start。在我的core.php中,我使用cake作为会话处理程序。我看到autologin正在保存cakephp.cookie,还有phpsessid,当浏览器死机时,它甚至会删除cookie!有什么想法吗?试试这个链接:上面的代码在我的机器上运行良好。然而,你也可以尝试做一个相反的:哇!这是一个巨大的变化。让我试试看。请耐心听我说。你的密码确实帮我找到了(+1)个地方。然而,在我结束这个问题之前的最后一个查询是,我正在使用FacebookPluginforAuth(FBConnect),它有一行名为Session\u start。在我的core.php中,我使用cake作为会话处理程序。我看到autologin正在保存cakephp.cookie,还有phpsessid,当浏览器死机时,它甚至会删除cookie!有什么想法吗?试试这个链接:上面的代码在我的机器上运行良好。但是,您也可以尝试将以下内容反转: