cakephp不区分大小写登录


cakephp case insensitive login

我试图使用户名大小写不敏感,但我仍然希望以注册用户名的方式存储用户名。因此,如果约翰登录,他可以键入"约翰"和他的密码。

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());   
        } else {
            $this->Session->setFlash('Your username/password combination was incorrect');
        }
    }
}

我会以某种方式调整上面的代码还是执行自定义SQL查询?

任何帮助都将不胜感激。非常感谢

他的意思是将原始的"John"存储在数据库中,然后strtolower数据库保存的值和登录表单上输入的名称。然后你仍然拥有与注册时相同的用户名。

添加示例:

登录ctp

echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'register')));
echo $this->Form->input('User.username', array('label' => 'First Name:'));
echo $this->Form->input('User.password', array('label' => 'Password', 'type' =>   'password', 'value' =>  false));
echo $this->Form->submit('Register');
echo $this->Form->end();

考虑一下,当用户输入用户名和密码时。假设我使用的用户名是"John"。大写字母J是我们要确保在数据库中的。在保存数据时,我们不会使用strtolower。因此,通过使用cake的save()方法,我们可以完成保存区分大小写的问题。

寄存器.ctp

public function register()
{
    if ($this->Auth->loggedIn()) {
        $this->redirect(array('controller' => 'users', 'action' => 'login'));
    }
    if ($this->request->is('post')) {
       if ($this->User->User->saveAll($this->request->data)
       {
           $this->Session->setFlash(__('Your account has been created', true));
           $this->redirect(array('controller' => 'users', 'action' => 'index'));
       }}

现在,当我们进行登录操作时:

if ($this->request->is('post')) {
 if ($this->Auth->loggedIn()) {
       $logged = $this->User->query('Your sql query matching username/password with strtolower, if you need to implement security hash from cakephp you can do that through cakephp hash method');
     }
 }

基本示例,但它应该有助于

在保存(或检查)之前只需使用strtolower php函数。

验证登录时,执行以下操作:

$query = "SELECT * FROM table WHERE LOWER(username) = '". strtolower($username)."'";

这将使您的列、用户名小写,并计算它是否等于小写$username。这样,您就可以将任何案例存储在数据库中。

CakePHP不区分大小写。它构建了一个非常通用的查询,根据数据库中的用户名来检查您的条目。

$conditions = array(
    $model . '.' . $fields['username'] => $username,
    $model . '.' . $fields['password'] => $this->_password($password),
);

很可能是数据库设置导致您的设置区分大小写。

例如,在我的所有网站上,我的用户名都是不区分大小写的,而且我对代码没有做任何特别的处理。

在调用$this->Auth->login()之前,添加此行:

$this->request->data['User']['username'] = strtolower($this->request->data['User']['username']);

简单!