2015-01-01 24 views
5

この問題を解決するには? は、私はここで「call of undefined function hash_equals()」 のエラーを取得しています私のコードです:定義されていない関数hash_equals()のPHP呼び出し

$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 
} 

私は何が起こっているのを知って、私はちょうどこの機能を探したが、それは代わりに私の問題を引けるいけません。 私の悪い英語のために申し訳ありません。ありがとうございました! the documentationから

+0

あなたのPHPのバージョンは何ですか? – sectus

答えて

18

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

+0

'hash_equals'は[password_compat]にありません(https://github.com/ircmaxell/password_compat) – t1gor

8

(PHP 5> = 5.6.0)

あなたが5.6.0よりも古いバージョンを持っているなら、あなたはこの機能を内蔵していません。

0

phpinfo()を使用して、PHPのバージョンを確認してください。 5.6より前のPHPバージョンには、hash_equals関数が組み込まれていません。この機能を使用するには、PHPの最新バージョン(または少なくとも5.6)にアップグレードしてください。私たちは、古いバージョンのPHPのための置換機能を見つけることができますphp.netドキュメントで

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; 
     } 
    } 
} 

これはおそらく優れている - あなたは小さなが必要テーブルは2列(ip textrecent_datetime datetime)のdosと呼ばれ、recent_datetimeを定義する必要があります。独自のSQLエンジンを追加します。私はmysql文をあらかじめ準備していません。それは見やすく、あなたが好きなら擬似コードと見なします。だから落ち着いてください。あなたの人生は終わらないでしょう。私は非常にあなたがサーバーのIPアドレス変数を注入することができます疑い、あなたがZendがmuppetsできる場合。とにかく私は逃げる。

あなたはかなりOKです、このから認証ハンマー保護を取得します。

関連する問題