0

PDO を使用するように記述されていることがわかった関数があり、codeigniters アクティブ レコード db クラスを使用するように変更しました。次のように関数内にコードを配置すると、すべてが機能します。

    function login_attempt_count() {

      $seconds = 10;
         // First we delete old attempts from the table 
      $oldest1 = strtotime(date('Y-m-d H:i:s').' - '.$seconds.' seconds');
      $oldest2 = date('Y-m-d H:i:s',$oldest1);
      $del_data = $oldest2;
      $this->db->where('when <', $del_data);
      $this->db->delete('Login_Attempts');
        // Next we insert this attempt into the table
      $data = array(
      'ip' => $_SERVER['REMOTE_ADDR'],
      'when' => date("Y-m-d H:i:s"));
      $this->db->insert('Login_Attempts', $data);

        // Finally we count the number of recent attempts from this ip address 
      $count = 'SELECT count(*) as number FROM Login_Attempts WHERE ip = ?';
      $num = $this->db->query($count, $_SERVER['REMOTE_ADDR']);
      if ($num->num_rows() > 0){
        foreach($num->result() as $attempt){
          $attempts = $attempt->number;
            return $attempts;
        }
      }
 }   

次のように使用します。

    $max_attempts = 3;

    if(login_attempt_count() <= $max_attempts) {
        echo 'login';
    }else{
        echo 'To many attempts';
    } 

またはこれ:

 $a = login_attempt_count();

ページの残りの部分が読み込まれないようにします。したがって、これはエラーを示します。繰り返しますが、関数内で関数の外でコードを使用すると、期待どおりに機能します。

または、私が使用すべき完全に優れた安全な方法がある場合は、提案をお待ちしています. ありがとう!

4

1 に答える 1

0

ここで私は自分の質問に答えています。;) 関数は正常に動作します! ただし、ビューでテストするとエラーが発生しますか? モデル内に関数を配置し、コントローラー内で関数を呼び出すと、うまく機能します。次のように使用します。

class Auth extends CI_Controller{

   function login(){

   $max_attempts = 3;
        if($this->auth_model->login_attempt_count() <= $max_attempts) {
          //Do something
        }else{
          //Something else
        } 
   }//End Function
}//End Class
于 2012-10-29T01:24:11.197 に答える