2014-06-17 52 views
0

你好,我是新來cakephp 我的問題是$this->Auth->login($this->request->data)總是返回true,我不知道它是否有什麼關係與表名稱,我的表名稱是「管理員」而不是「用戶」,它接受任何密碼,事情真的是錯誤的。登錄cakephp不工作我沒有(管理員)表而不是(用戶)

這裏是我的登錄功能:

public function login() 
    { 
     $this->layout = 'login'; 
     if ($this->request->is('post')) 
     { 

      if ($this->Auth->login()) 
      { 
       return $this->redirect($this->Auth->redirect()); 
      } 
      else 
      { 
       $this->Session->setFlash(__('Username or password is incorrect')); 
      } 
     } 
    } 

我把該功能是here這不是在在所有

登錄我的舊代碼爲:

public function login() 
    { 
     $this->layout = 'login';    
     if(!$this->Session->read('Auth.User')) 
     { 
      debug($this->Session->read('Auth.User')); 
      if ($this->request->is('post')) 
      { 
       debug($this->Auth->login()); 
       //$this->pa($this->request->data); 

       if ($this->Auth->login()) 
       { 
        $this->Admin->id = $this->Session->read('Auth.Admin.id'); 
        $this->redirect($this->Auth->redirect()); 
       } 
       else 
       {      
        $this->Session->setFlash(__('Username or password is incorrect')); 
       } 
      } 
     } 
     else 
     { 
      return $this->redirect($this->Auth->redirect()); 
     } 
    } 

它不工作太

+0

請註明您的確切的CakePHP版本,相應地標記您的問題,並根據所閱讀的版本** http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-在** – ndm

+0

我的cakephp版本是:2.4.4 –

+0

只能用 $ this-> Auth-> login(); 它的自動驗證 –

回答

0

您需要更改Auth模型。將此添加到您的控制器並嘗試。

function beforeFilter() { 
    $this->Auth->userModel = 'admins'; 
} 

此外,如果你有比用戶名和密碼等不同的領域,你需要將它們作爲很好地驗證這樣的事情

$this->Auth->fields = array(
     'username' => 'your_username_field', 
     'password' => 'your_password_field' 
    ); 

希望這將有助於

+0

添加該行「$ this-> Auth-> userModel ='admins';」在其中控制器 –

+0

'$ this-> Auth-> fields = array( 'username'=> $ this-> request-> data ['Admin'] ['username'], 'password'=> $ this-> > request-> data ['Admin'] ['password'] );' –

相關問題