2016-06-14 57 views
0

我想比較輸入「傳遞」與數據庫中的「傳球」。如果匹配,用戶將進入註冊表單。相同的密碼值比較返回false cakephp

即使兩個密碼的值相同,但我仍然得到'密碼不正確'的錯誤結果,並且提示:Undifined index:pass。 我使用CakePHP 2.8

這是我現在的代碼:

public function checkCodeRespondent() { 

    $password = $this->data['Respondent']['pass']; 

     if (isset($password) && !empty($password)) 
     { 

      $respondent = $this->findByPass($password); 
      debug($respondent); 

      if ($this->data['Respondent']['pass'] != $respondent['pass']) { 


       print 'password is not correct'; 
      } else { 
       print 'password is correct'; 
      } 


     } 

這是從數據庫

public function findByPass($pass) { 

    $respondent = $this->Respondent->find('first', array('conditions' => array('pass' => $pass))); 
    return $respondent; 
} 
+0

沒有兩個都沒有散列。 – user2314339

回答

1

如果您的密碼是不是哈希只是嘗試:

public function checkCodeRespondent() { 
$password = $this->data['Respondent']['pass']; 
$condition = array('Respondent.pass'=>$password); 
if($this->Respondent->hasAny($condition)){ 
print 'password is correct'; 
     } else { 
      print 'password is not correct'; 
     } 
} 

你不需要多功能所有..

+0

Thx很多。它正在工作。所以這隻適用於不散列? – user2314339

+0

當然不是,這適用於所有的時間...我只是說如果你的密碼被哈希,我會添加另一個條件太.. –

+0

只是爲了好奇:)你會如何添加條件?也許它會派上用場的另一個項目 – user2314339

0

Undefined index 'pass'

檢索數據的功能

表示數組引用Key值e尚未設置,所以$this->data['Respondent']['pass']值未設置或$respondent['pass']尚未設置。檢查兩者並查看哪一個丟失,然後退後找到丟失數據的原因。

調試: print_r($this->data);輸出是什麼? print_r($respondent);輸出是什麼,都是你期望的形狀?

注意:還因爲它們是密碼: 這些值中的任何一個是否散列?如果是這樣,他們哈希?

+0

Thx非常適合您的快速反應:D您是對的$ respondent ['pass']的值爲null。 這有點奇怪,因爲在$ respondent中設置了該值。我錯過了什麼? – user2314339

+0

@ user2314339確定'debug'函數是否清除變量值? – Martin

+0

@ user2314339所以下一階段是檢查爲什麼' - > findByPass($ password)'沒有返回有效值 – Martin

0

我做了一個錯字:)

此:

if ($this->data['Respondent']['pass'] != $respondent['pass']) 

應該是這樣的:

if ($this->data['Respondent']['pass'] != $respondent['Respondent']['pass']) 

現在的比較很有魅力