PHP调用未定义函数hash_equals()

时间:2015-01-01 06:10:41

标签: php mysql

任何人都可以帮我解决这个问题吗? 我收到错误的call of undefined function hash_equals()' 这是我的代码:

$username = 'Admin';
$password = 'sample1Pasword';

$dbh = new PDO('mysql:host=localhost;dbname=test', $USER, $PASSWORD);

$sth = $dbh->prepare('
  SELECT
    hash
  FROM users
  WHERE
    username = :username
  LIMIT 1
  ');

$sth->bindParam(':username', $username);

$sth->execute();

$user = $sth->fetch(PDO::FETCH_OBJ);

// Hashing the password with its hash as the salt returns the same hash
if ( hash_equals($user->hash, crypt($password, $user->hash)) ) {
  // Ok!
}else{
  //user not found
}

我不知道发生了什么,我只是搜索这个功能,但它给了我一个问题。 对不起,我的英语不好。谢谢!

5 个答案:

答案 0 :(得分:26)

php.net文档中,我们可以找到旧版php的替换函数:

if(!function_exists('hash_equals'))
{
    function hash_equals($str1, $str2)
    {
        if(strlen($str1) != strlen($str2))
        {
            return false;
        }
        else
        {
            $res = $str1 ^ $str2;
            $ret = 0;
            for($i = strlen($res) - 1; $i >= 0; $i--)
            {
                $ret |= ord($res[$i]);
            }
            return !$ret;
        }
    }
}

否则,您可以使用此库:https://github.com/ircmaxell/password_compat

答案 1 :(得分:8)

来自the documentation

  

(PHP 5> = 5.6.0)

如果您的版本低于5.6.0,那么您将不会内置此功能。

答案 2 :(得分:0)

使用phpinfo()并检查您的PHP版本。 5.6之前的PHP版本没有内置hash_equals功能。升级到最新版本的PHP(或至少5.6)以使用此功能。

答案 3 :(得分:0)

if(!function_exists('hash_equals'))
{
    function hash_equals($a,$b){
        /***
        * special delay incrementing dos robust comparator
        */  
        $ip_one = $_SERVER['HTTP_X_FORWARDED_FOR'];
        $ip_two = $_SERVER['REMOTE_ADDR'];
        $db = new SQLdb();
        $counter = $db->quickquery("SELECT count(*) FROM dos WHERE (ip='".$ip_one."' OR ip='".$ip_two."') AND  recent_datetime >= DATE_SUB(NOW(), INTERVAL 1 HOUR)");
        if ($counter > 5) {
            sleep($counter *2);
        }
        if ($a!=b){
            $db->quickquery("INSERT INTO dos (ip, recent_datetime) VALUES ('".$ip_one."', now())");
            $db->quickquery("INSERT INTO dos (ip, recent_datetime) VALUES ('".$ip_two."', now())");
            return false;
        }
        else{
            return true;
        }
    }
}

这可能更好 - 您需要一个名为dos的小表,其中包含2列(ip textrecent_datetime datetime),您应该确定索引recent_datetime。并添加自己的SQL引擎。我没有预先准备mysql语句。它很清晰,如果你愿意,可以考虑伪代码。好冷静下来。你的生活不会结束。我非常怀疑你可以注入服务器的IP地址变量,如果你可以Zend是muppets。无论如何我离题了。

你将从中获得认证锤保护,这是相当不错的。

答案 4 :(得分:0)

来自Facebook SDK的解决方案:  https://github.com/facebook/php-graph-sdk/blob/5.x/src/Facebook/polyfills.php

public override async Task<bool> ExecuteTests()
{
    HttpResponseMessage res =  await  HttpClientConnection.GetAsync("Default/GetCustomerInfo/Id")
    res.EnsureSuccessStatusCode();
    if(res.IsSuccessStatusCode)
    {
        return true;
    }
}