2015-01-01 3 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 버전을 확인하십시오. PHP 버전 5.6 이전에는 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; 
     } 
    } 
} 

을 - 당신은 작은 필요 두 열 (ip text, recent_datetime datetime)이있는 도스라는 테이블을 사용하고 있으며 인덱스는 recent_datetime이어야합니다. 그리고 자신의 SQL 엔진을 추가하십시오. 나는 mysql 문을 미리 준비하지 않았다. 원한다면 읽을 수 있고 의사 코드로 간주하십시오. 그래서 진정 해요. 너 인생 끝나지 않을거야. 나는 서버 IP 주소 변수를 주입 할 수 있을지 의심 스럽지만 Zend가 muppets 일 수 있다면. 어쨌든 나는 빗 나간다.

인증 망치 보호 기능이 제공됩니다.이 기능은 상당히 괜찮습니다.

관련 문제