├── LICENSE ├── README.md └── ratelimit_helper.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Alexandru Gaidei 2 | http://alexandru.master.pro.md/ 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Codeigniter rate limiter helper 2 | =============== 3 | 4 | Small helper for allow only a certain number of requests per a certain amount of minutes. 5 | 6 | The code is released under an MIT license. 7 | 8 | Usage 9 | ----- 10 | 11 | ```php 12 | public function __construct() 13 | { 14 | parent::__construct(); 15 | 16 | $this->load->helper('ratelimit'); 17 | limitRequests($this->input->ip_address()); 18 | } 19 | ``` 20 | 21 | By default is limit to 100 requests per 5 min, but you can indicates it in params: 22 | 23 | ```php 24 | limitRequests("key", 50, 120); 25 | ``` 26 | 27 | where 50 is number of frequests and 120 is time in seconds. 28 | -------------------------------------------------------------------------------- /ratelimit_helper.php: -------------------------------------------------------------------------------- 1 | load->driver('cache', array('adapter'=>'file')); 35 | 36 | $cache_key = $ip . "_key"; 37 | $cache_remain_time = $ip . "_tmp"; 38 | 39 | $current_time = date("Y-m-d H:i:s"); 40 | 41 | //if first request 42 | if ($CI->cache->get($cache_key) === false){ 43 | 44 | $current_time_plus = date("Y-m-d H:i:s", strtotime("+".$sec." seconds")); 45 | 46 | $CI->cache->save($cache_key, 1, $sec); 47 | $CI->cache->save($cache_remain_time, $current_time_plus, $sec * 2); 48 | } 49 | else{ 50 | 51 | $requests = $CI->cache->get($cache_key); 52 | 53 | $time_lost = $CI->cache->get($cache_remain_time); 54 | 55 | if($current_time > $time_lost){ 56 | 57 | //as first time request 58 | $current_time_plus = date("Y-m-d H:i:s", strtotime("+".$sec." seconds")); 59 | 60 | $CI->cache->save($cache_key, 1, $sec); 61 | $CI->cache->save($cache_remain_time, $current_time_plus, $sec * 2); 62 | 63 | } 64 | else{ 65 | $CI->cache->save($cache_key, $requests + 1, $sec); 66 | } 67 | 68 | $requests = $CI->cache->get($cache_key); 69 | if($requests > $max_requests){ 70 | header("HTTP/1.0 429 Too Many Requests"); 71 | exit; 72 | } 73 | 74 | } 75 | 76 | } 77 | 78 | } --------------------------------------------------------------------------------