1

我正在使用带有自动登录组件的 CakePHP 2x。问题是,我可以写这些东西,但是我不确定如何实现它来读取和授权。当用户到达该页面时,他的浏览器中仍然有 cookie,但是,我该如何授权呢?

我的登录脚本:

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');
            }
        }
4

1 回答 1

1

这应该是这样的:

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());
        }
    }
}

这让您了解如何完成相同的任务,但是,我根据我的数据库结构使用了表单字段。

请根据您的数据库结构更改表单字段。

于 2012-09-06T10:12:51.683 回答