2012-07-16 102 views
1

我試圖使用戶名的情況下不敏感但我仍然想以他們的註冊相同的方式存儲用戶名。所以如果John要登錄,他可以輸入'john'和他的密碼。cakephp不區分大小寫的登錄

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查詢?

任何幫助將不勝感激。非常感謝

+0

只是使用正確的數據庫排序規則,您將始終有不區分大小寫的查詢,例如, utf8_unicode_ci(ci代表正是這樣) – mark 2012-07-17 07:50:14

回答

3

他的意思是在數據庫中存儲原始的「John」,然後在登錄表單中輸入數據庫保存的值以及輸入的名稱。然後,您仍然擁有與註冊方式相同的用戶名。

實施例加了:

Login.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()方法,我們可以完成保存你的大小寫敏感問題。

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

基本的例子,但它應該幫助

+0

是的,這是我想要做的,但它是如何與cakephp :( – Tim 2012-07-16 18:46:13

+0

@Tim我會發佈一個小例子,讓你開始 – Sixthpoint 2012-07-16 18:48:51

1

只需使用strtolower php函數保存它(或檢查它)之前。

+0

「但我仍然想以與註冊相同的方式存儲用戶名」 – Jay 2012-07-16 18:22:37

+0

好的。我的聲明仍然適用^ _^ – Neal 2012-07-16 18:22:58

+0

請詳細說明。如果你是strtolower('John'),那麼只有'john'將被保存到數據庫中,而不是'John'。 – Jay 2012-07-16 18:23:57

1

當您驗證您的登錄做這樣的事情:

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

這會將您的列和用戶名縮寫爲小寫,並評估它是否等於小寫字母$username。這樣,你可以在數據庫中存儲任何文件。

1

CakePHP不區分大小寫。它構建了一個非常通用的查詢來檢查您的輸入與數據庫中的用戶名。

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

這可能是一個數據庫設置,導致您的區分大小寫。

例如,在我所有的網站上,我的用戶名不區分大小寫,並且我沒有對代碼做任何特殊處理。

+0

有更多的蛋糕版本的排序規則,不是嗎? – tigrang 2012-07-16 20:11:53

+0

@tigrang - 可能是 - 就像總是包含我測試過的版本一樣 - 即使它是非常不相關的,就像在這種情況下一樣。) – Dave 2012-07-16 20:13:21

+0

「CakePHP的身份驗證登錄是ALREADY不區分大小寫。」是不準確的,Cake沒有做任何特殊的事情來使它變得不敏感,這一切都是關於整理。 Cake只是做'userfield'='exactlyWhatUserTyped''; – tigrang 2012-07-16 20:15:57

0

之前調用$這個 - > Auth->登錄(),加入這一行:

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

簡單!