2014-09-30 109 views
1

我使用CakePHP的最後一個版本(2.5.4,如果我是對的)&我嘗試登錄時遇到問題。CakePHP登錄不認證

我有一張名爲「Accounts」的表,&我的控制器被稱爲「UsersController.php」,model =>「User.php」等。似乎沒有問題,因爲我設置了默認表「賬戶」。我已經提出了註冊的意見,並且我正在使用md5來散列密碼,並且它工作的很好。

但是,當我想登陸,它始終返回false &它不記錄我

這裏是UsersController.php我的登錄功能:

function login(){ 
     if($this->request->is('post')) 
     { 
      if($this->Auth->login()) 
      { 
       $this->Session->setFlash("Vous êtes maintenant connecté !", "notif"); 
       $this->redirect('/'); 
      } else { 
       $this->Session->setFlash("Les identifiants sont incorrects", "notif", array('type' => 'danger')); 
       debug(Security::hash($this->request->data['User']['PasswordHash'])); 
      } 
     } 
    } 

這裏是形式從login.ctp:

<?= $this->Form->input('Login', array('placeholder' => 'Login', 'label' => false)); ?> 
            <b class="tooltip tooltip-bottom-right">Votre nom de compte</b> 
           </label> 
          </section> 
          <section> 
           <label class="input"> 
            <i class="icon-append fa fa-lock"></i> 
            <?= $this->Form->input('PasswordHash', array('placeholder' => 'Mot de passe', 'label' => false)); ?> 
            <b class="tooltip tooltip-bottom-right">Votre mot de passe!</b> 
           </label> 
          </section> 
         </fieldset> 
         <fieldset> 
          <section> 
           <h4>Mot de passe perdu ?</h4> 
           <p><a style='color:#009900' href="#">Cliquez ici</a> pour réinitialiser le mot de passe.</p> 
          </section> 
         </fieldset> 
        <footer> 
         <center> 
          <?php $options = array(
           'class' => 'btn-u', 
           'label' => 'Connexion' 
          ) ?> 
          <?= $this->form->end($options); ?> 

我忘了提,我的專欄不是USERNAME &密碼,而是登錄& PasswordHash

而且,這裏是我的AppController.php:

class AppController extends Controller { 

public $components = array(
    'Session', 
    'Cookie', 
    'Auth'); 

function beforeFilter(){ 
    Security::setHash('md5'); 
    $this->Auth->allow(); 
    $this->Auth->authenticate = array(
     'Form' => array(
      'fields' => array('username' => 'Login', 'password' => 'PasswordHash'), 
     ) 
    ); 
} 

}

我無法登錄,我不明白爲什麼。我的請求查詢如下所示:

SELECT `User`.`Id`, `User`.`Login`, `User`.`PasswordHash`, `User`.`Nickname`, `User`.`Role`, `User`.`AvailableBreeds`, `User`.`Ticket`, `User`.`SecretQuestion`, `User`.`SecretAnswer`, `User`.`Lang`, `User`.`Email`, `User`.`CreationDate`, `User`.`Tokens`, `User`.`NewTokens`, `User`.`LastVote`, `User`.`RecordVersion` FROM `madara`.`accounts` AS `User` WHERE `User`.`Login` = 'Twoast' LIMIT 1 

調試顯示我與我的表帳戶中的哈希相同。我不確定我做錯了什麼。

在此先感謝!

+0

你檢查了你的服務器上的PHP錯誤日誌? – 2014-09-30 20:15:27

+0

我的PHP錯誤日誌中沒有任何東西,似乎沒有錯誤,請求看起來很棒,但它不需要。該死...... – Twoast 2014-10-01 11:22:26

+0

你有沒有機會登錄? _(我認爲如果你已經登錄,'login'返回false。)_我的登陸方法首先檢查'$ this-> Auth-> loggedIn()'在任何其他事務處理之前是否爲false。 – AgRizzo 2014-10-01 13:49:38

回答

0

您在表格中設置了密碼字段的大小是多少?它足夠長以適應散列字段嗎?我之前有過這種情況,必須將其設置爲至少50個字符。

+0

這應該是一個*評論* **不是**一個*答案*。 – burzum 2014-09-30 22:56:08

+0

是的,它足夠長(45個字符),我不明白,導致PasswordHash字段的調試完全符合我在數據庫中的PasswordHash值。 – Twoast 2014-10-01 11:23:25

+0

我無法評論帖子,所以我必須發表一個答案。就像我所說的,我將數據庫中密碼字段的大小更改爲128,併爲我解決了這個問題。 – user3363286 2014-10-01 16:37:01

0

因此,3天后,我終於找到了解決方案。

我的功能登錄成了:

function login(){ 
     if($this->request->is('post')) 
     { 
      $data = $this->request->data; 
      $Logined = $this->request->data['User']['Login']; 
      $PasswordHashed = Security::hash($data['User']['PasswordHash'], 'md5', false); 

      $user = $this->User->find('first', array(
       'conditions' => array(
        'User.Login' => $Logined, 
        'User.PasswordHash' => $PasswordHashed 
       ), 
       'recursive' => -1 
      )); 
      if(!empty($user) && $this->Auth->login($user['User'])) { 
       $this->Session->setFlash("Vous êtes maintenant connecté !", "notif"); 
       $this->redirect('/'); 
      } else { 
       $this->Session->setFlash("Les identifiants sont incorrects", "notif", array('type' => 'danger')); 
      } 
     } 
    } 

所以,這個功能效果很好,它只是做了手動登錄。

感謝您的幫助! :)