2016-04-29 29 views
0

私はログインを抑制するためのこの関数を持っていますが、問題があり、関数内でpdo接続が機能しない場合は、 "undefined $ conn"私はそれが範囲に起因する正しい場合、nullの関数 "、これの周りには何か問題がありますか?pdo接続がPHP関数で動作していません

<?php 
function check(){ 
    function get_multiple_rows($getfailed) { 
     $rows = array(); 
     while($row = $getfailed->fetch(PDO::FETCH_ASSOC)) { 
      $rows[] = $row; 
     } 
     return $rows; 
    } 
    $throttle = array(1 => 1, 5 => 2, 30 => 10); 
    if ($getfailed = $conn->query("SELECT MAX(attempted) AS attempted FROM failed_logins")){ 
     $rows = get_multiple_rows($getfailed); 
     $getfailed->closeCursor(); 
     $latest_attempt = (int) date('U', strtotime($rows[0]['attempted'])); 
     if ($getfailed = $conn->query("SELECT COUNT(1) AS failed FROM failed_logins WHERE attempted > DATE_SUB(NOW(), INTERVAL 15 minute)")){ 
      $rows = get_multiple_rows($getfailed); 
      $getfailed->closeCursor(); 
      $failed_attempts = (int) $rows[0]['failed']; 
      krsort($throttle); 
      foreach ($throttle as $attempts => $delay){ 
       if ($failed_attempts > $attempts) { 
        $remaining_delay = (time() - $latest_attempt) - $delay; 
        if ($remaining_delay < 0){ 
         echo "You have exceeded the login attempts limit"; 
        } 
        return false; 
        break; 
       }else{ 
        return true; 
       } 
      } 
     } 
    } 
} 
?> 
+0

$ connはどこで初期化されていますか?他のファイル?属性として設定できるクラスはありますか? –

+0

[PDO例外](http://php.net/manual/en/pdo.error-handling.php)をオンにすると、エラーがより明らかになります。 – tadman

+0

@ Sergi Case、はいそれは別のファイルであり、クラスにあります – Serjio

答えて

0

あなたはそうのようなあなたのチェック関数は、引数を受け取る作るために試みることができる:

<?php 
    function check(PDO $conn){ 
     function get_multiple_rows(PDOStatement $getfailed) { 
      $rows = array(); 
      while($row = $getfailed->fetch(PDO::FETCH_ASSOC)) { 
       $rows[] = $row; 
      } 
      return $rows; 
     } 
     $throttle = array(1 => 1, 5 => 2, 30 => 10); 
     if ($getfailed = $conn->query("SELECT MAX(attempted) AS attempted FROM failed_logins")){ 
      $rows = get_multiple_rows($getfailed); 
      $getfailed->closeCursor(); 
      $latest_attempt = (int) date('U', strtotime($rows[0]['attempted'])); 
      if ($getfailed = $conn->query("SELECT COUNT(1) AS failed FROM failed_logins WHERE attempted > DATE_SUB(NOW(), INTERVAL 15 minute)")){ 
       $rows = get_multiple_rows($getfailed); 
       $getfailed->closeCursor(); 
       $failed_attempts = (int) $rows[0]['failed']; 
       krsort($throttle); 
       foreach ($throttle as $attempts => $delay){ 
        if ($failed_attempts > $attempts) { 
         $remaining_delay = (time() - $latest_attempt) - $delay; 
         if ($remaining_delay < 0){ 
          echo "You have exceeded the login attempts limit"; 
         } 
         return false; 
         break; 
        }else{ 
         return true; 
        } 
       } 
      } 
     } 
    } 

は今、あなたはそうのようなあなたの関数に$のCONNを渡すことができます。

<?php 
    check($conn); 

・ホープ、このことができます。..

0

明らかに、関数内に$connを定義していません。要求を送信するには、ローカルで$connを定義しなければなりません。this answerを読んで、WebアプリケーションでPDO接続を適切に設定することを検討する必要があります。

+0

あるいは、関数に引数として '$ conn'を渡すことができます。 @Rickありがとうございます。 – Rick

+0

はい、これもオプションになります。 –

+0

@Rick、それはいい考えですが、クエリのエラーはどうですか? – Serjio

関連する問題