PHP字符串不相等但strpos和length相同

时间:2012-04-02 17:03:26

标签: php string operators

以下代码生成结果“我不明白这一点”。有什么想法吗?如果两个字符串的长度相同,则匹配第一个字符,实际上在视觉上是相等的,为什么它们与等号运算符不相等?

$userid = (int) $_POST['username'];
$password = trim($this->input->post('password'));
$hashed = trim(md5($password . $this->salt));
$success = false;
if( $this->users[$userid]->password == $password ) {
    $success = true;
}
else {
    if( strpos($hashed, $this->users[$userid]->password) === 0 && 
        strlen($hashed) == strlen($this->users[$userid]->password) ) {
        echo "I don't understand this";
    }
}

请忽略使用md5哈希密码来回答此问题的可取性。

使用PHP5.3.2

1 个答案:

答案 0 :(得分:1)

我在想你是否将密码存储为哈希?您需要将用户的密码与哈希值进行比较,而不是密码本身。

改变这个:

if( $this->users[$userid]->password == $password ) {

到此:

if( $this->users[$userid]->password == $hashed ) {