ratelimit_helper.php
//limit for IP to 100 requests per 5 min
limit for IP to 100 requests per 5 min
limit for IP to 100 requests per 5 min
function limitRequests($ip, $max_requests = 100, $sec = 300){
        $CI =& get_instance();
        $CI->load->driver('cache', array('adapter'=>'file'));
        $cache_key = $ip . "_key";
        $cache_remain_time = $ip . "_tmp";
        $current_time = date("Y-m-d H:i:s");
        if ($CI->cache->get($cache_key) === false){
            $current_time_plus = date("Y-m-d H:i:s", strtotime("+".$sec." seconds"));
            $CI->cache->save($cache_key, 1, $sec);
            $CI->cache->save($cache_remain_time, $current_time_plus, $sec * 2);
        }
        else{
            $requests = $CI->cache->get($cache_key);
            $time_lost = $CI->cache->get($cache_remain_time);
            if($current_time > $time_lost){
                $current_time_plus = date("Y-m-d H:i:s", strtotime("+".$sec." seconds"));
                $CI->cache->save($cache_key, 1, $sec);
                $CI->cache->save($cache_remain_time, $current_time_plus, $sec * 2);
            }
            else{
                $CI->cache->save($cache_key, $requests + 1, $sec);
            }
            $requests = $CI->cache->get($cache_key);
            if($requests > $max_requests){
                header("HTTP/1.0 429 Too Many Requests");
                exit;
            }
        }
    }
function limitRequests ( $ ip , $ max_requests = 100 , $ sec = 300 ) { $ CI = & get_instance ( ) ; $ CI -> load -> driver ( 'cache' , array ( 'adapter' => 'file' ) ) ; $ cache_key = $ ip . " _key " ; $ cache_remain_time = $ ip . " _tmp " ; $ current_time = date ( " Y-m-d H:i:s " ) ; if ( $ CI -> cache -> get ( $ cache_key ) === false ) { $ current_time_plus = date ( " Y-m-d H:i:s " , strtotime ( " + " . $ sec . "  seconds " ) ) ; $ CI -> cache -> save ( $ cache_key , 1 , $ sec ) ; $ CI -> cache -> save ( $ cache_remain_time , $ current_time_plus , $ sec * 2 ) ; } else { $ requests = $ CI -> cache -> get ( $ cache_key ) ; $ time_lost = $ CI -> cache -> get ( $ cache_remain_time ) ; if ( $ current_time > $ time_lost ) { $ current_time_plus = date ( " Y-m-d H:i:s " , strtotime ( " + " . $ sec . "  seconds " ) ) ; $ CI -> cache -> save ( $ cache_key , 1 , $ sec ) ; $ CI -> cache -> save ( $ cache_remain_time , $ current_time_plus , $ sec * 2 ) ; } else { $ CI -> cache -> save ( $ cache_key , $ requests + 1 , $ sec ) ; } $ requests = $ CI -> cache -> get ( $ cache_key ) ; if ( $ requests > $ max_requests ) { header ( " HTTP/1.0 429 Too Many Requests " ) ; exit ; } } }
limit for IP to 100 requests per 5 min
limit for IP to 100 requests per 5 min
//if first request //as first time request