未定义函数hash_equals()的PHP调用

未定义函数hash_equals()的PHP调用,php,mysql,Php,Mysql,有人能帮我解决这个问题吗? 我得到了“调用未定义函数hash_equals()”的错误 这是我的密码: $username = 'Admin'; $password = 'sample1Pasword'; $dbh = new PDO('mysql:host=localhost;dbname=test', $USER, $PASSWORD); $sth = $dbh->prepare(' SELECT hash FROM users WHERE userna

有人能帮我解决这个问题吗? 我得到了“
调用未定义函数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
}
我不知道发生了什么,我只是搜索这个函数,但它给了我一个问题。 对不起,我的英语不好。谢谢大家!

来自:

(PHP 5>=5.6.0)

如果您的版本早于5.6.0,则不会内置此函数。

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

在文档中,我们可以找到旧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;
        }
    }
}
否则,您可以使用此库:

这可能更好-您需要一个名为dos的小表,它有两列(
ip text
recent\u datetime
),并且您应该明确地索引
recent\u datetime
)。并添加您自己的sql引擎。我没有预先准备mysql语句。它是易读的,如果你喜欢的话,可以考虑伪代码。冷静点。你的生命不会结束。我非常怀疑您是否可以注入服务器ip地址变量,如果可以,Zend是木偶。无论如何,我离题了


您将从中获得身份验证锤保护,这很正常。

解决方案取自Facebook sdk:

/**
*版权所有2017 Facebook,Inc。
*
*特此授予您非独家的、全球范围内的、免版税的许可证
*以源代码或二进制文件的形式使用、复制、修改和分发此软件
*用于与由提供的web服务和API相关的表单
*脸谱网。
*
*与任何与Facebook平台集成的软件一样,您的使用
*此软件的使用受Facebook开发者原则和
*政策[http://developers.facebook.com/policy/]. 本版权公告
*应包含在软件的所有副本或实质部分中。
*
*本软件按“原样”提供,无任何形式的明示或明示担保
*默示,包括但不限于适销性保证,
*适用于特定目的和非侵权。在任何情况下均不得
*作者或版权持有人应承担任何索赔、损害或其他责任
*无论是合同诉讼、侵权诉讼还是其他诉讼中产生的责任
*来自、不属于或与软件、使用或其他相关
*软件的交易。
*
*/
/**
*@见https://github.com/sarciszewski/php-future/blob/master/src/Security.php#L37-L51
*/
如果(!function_存在('hash_等于')){
函数hash_等于($knownString,$userString)
{
如果(函数_存在('mb_strlen')){
$kLen=MBstrlen($knownString,'8bit');
$uLen=mbstrlen($userString,'8bit');
}否则{
$kLen=strlen($knownString);
$uLen=strlen($userString);
}
如果($kLen!=$uLen){
返回false;
}
$result=0;
对于($i=0;$i<$kLen;$i++){
$result |=(ord($knownString[$i])^ord($userString[$i]);
}
//如果$result正好为0,则它们只是相同的字符串。。。
返回0=$result;
}
}

您的php版本是什么?
中没有hash_equals
请使用这种“安全”方法吗?是的,它是安全的,密码\u compat是一种替换方法
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;
        }
    }
}
/**
 * Copyright 2017 Facebook, Inc.
 *
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
 * use, copy, modify, and distribute this software in source code or binary
 * form for use in connection with the web services and APIs provided by
 * Facebook.
 *
 * As with any software that integrates with the Facebook platform, your use
 * of this software is subject to the Facebook Developer Principles and
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
 * shall be included in all copies or substantial portions of the software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */
/**
 * @see https://github.com/sarciszewski/php-future/blob/master/src/Security.php#L37-L51
 */
if (!function_exists('hash_equals')) {
    function hash_equals($knownString, $userString)
    {
        if (function_exists('mb_strlen')) {
            $kLen = mb_strlen($knownString, '8bit');
            $uLen = mb_strlen($userString, '8bit');
        } else {
            $kLen = strlen($knownString);
            $uLen = strlen($userString);
        }
        if ($kLen !== $uLen) {
            return false;
        }
        $result = 0;
        for ($i = 0; $i < $kLen; $i++) {
            $result |= (ord($knownString[$i]) ^ ord($userString[$i]));
        }
        // They are only identical strings if $result is exactly 0...
        return 0 === $result;
    }
}