在CakePHP';s验证组件

在CakePHP';s验证组件,cakephp,cakephp-1.3,cakephp-1.2,Cakephp,Cakephp 1.3,Cakephp 1.2,使用CakePHP的Auth组件,我如何允许用户使用“用户名”或“电子邮件”字段作为用户名,使用“密码”字段作为密码进行身份验证?同时使用(用户名和电子邮件)作为用户名是什么意思 编辑:好的,那么您想让Auth在数据库中的用户名和电子邮件字段中查找,以与用户输入的“用户名”进行比较?然后这样做: 要做到这一点,您必须跳过Auths autoredirect并自己管理它。这是用户\u控制器中的登录操作: public function login() { if(!empty($this-&

使用CakePHP的Auth组件,我如何允许用户使用“用户名”或“电子邮件”字段作为用户名,使用“密码”字段作为密码进行身份验证?

同时使用(用户名和电子邮件)作为用户名是什么意思

编辑:好的,那么您想让Auth在数据库中的用户名和电子邮件字段中查找,以与用户输入的“用户名”进行比较?然后这样做:


要做到这一点,您必须跳过Auths autoredirect并自己管理它。这是用户\u控制器中的登录操作:

public function login() {
    if(!empty($this->data)) { // Submitted form

        // Try to login with Email
        if(!$this->Auth->user() // if user wasn't logged in with username + pass
            && !empty($this->Auth->data['User']['username'])
            && !empty($this->Auth->data['User']['password'])
        ) {
            $user = $this->User->find('first', array(
                'conditions' => array(
                    'User.email' => $this->Auth->data['User']['username'],
                    'User.password' => $this->Auth->data['User']['password']
                ),
                'recursive' => -1
            ));

            if(!empty($user) && $this->Auth->login($user)) {
                // They logged in, so kill the flash error message
                $this->Session->delete('Message.auth');
            } else {
                $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth');
            }
        }

        if($this->Auth->user()) {
            // Post login logic here
            $this->redirect($this->Auth->redirect());
        }

    } else {
        if($this->Auth->user()) {
            $this->Session->setFlash(__d('users', 'You are already registered and logged in!', true));
            //$this->redirect('/');
            $this->redirect($this->Auth->redirect());
        }
    }
这是直接从我的应用程序复制的,所以可能需要对你的进行一些调整。不要忘记设置
$this->Auth->autoRedirect=false:beforeFilter()


您必须记住,Auth将自动检查用户名和密码,因此此操作仅从中提取。
Session::remove()
调用用于删除用户名/密码检查失败且电子邮件登录成功时自动留下的身份验证错误消息(否则,登录成功会收到错误消息).

我认为他的意思是使用用户名或电子邮件作为有效字段登录,这意味着可以使用电子邮件地址或用户名作为表单中的用户名字段。hm。。我漏掉了一个括号。说“代码不起作用”对我来说听起来很懒。我不是在用括号括起来,我的意思是说这个逻辑不起作用,syntex错误我总是忽略,但谢谢你的回复
public function login() {
    if(!empty($this->data)) { // Submitted form

        // Try to login with Email
        if(!$this->Auth->user() // if user wasn't logged in with username + pass
            && !empty($this->Auth->data['User']['username'])
            && !empty($this->Auth->data['User']['password'])
        ) {
            $user = $this->User->find('first', array(
                'conditions' => array(
                    'User.email' => $this->Auth->data['User']['username'],
                    'User.password' => $this->Auth->data['User']['password']
                ),
                'recursive' => -1
            ));

            if(!empty($user) && $this->Auth->login($user)) {
                // They logged in, so kill the flash error message
                $this->Session->delete('Message.auth');
            } else {
                $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth');
            }
        }

        if($this->Auth->user()) {
            // Post login logic here
            $this->redirect($this->Auth->redirect());
        }

    } else {
        if($this->Auth->user()) {
            $this->Session->setFlash(__d('users', 'You are already registered and logged in!', true));
            //$this->redirect('/');
            $this->redirect($this->Auth->redirect());
        }
    }