2016-06-14 50 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']) 

现在的比较很有魅力