PHP代碼,拒絕頻繁訪問

來源:互聯網
上載者:User

標籤:

一個網站效能有限,如果有人惡意去頻繁對頁面進行重新整理,其實對伺服器影響是很大的,導致資源使用非常高,直接影響到其他使用者的體驗。

那麼對於這樣的一些頻繁訪問,我們該如何去拒絕它呢?

我總結了兩種方法:第一種方式通過Web伺服器檢查拒絕,第二種方式通過代碼進行攔截過濾

通過Web伺服器檢查拒絕

第一種方式大致原理和思路是這樣的,比如我們的Web伺服器採用Nginx,那個Nginx可以記錄使用者訪問記錄,並通過查詢分析這個日誌可以得出頻繁訪問使用者IP列表。

我們需要做的事情:

做一個定時任務,定時去分析記錄檔,將得到的頻繁訪問使用者的IP,並將IP設定到Nginx設定檔中的IP黑名單中,然後進行reload設定檔,當IP類表較大時,可以更新到iptables中去。

記錄檔,查詢IP的訪問次數

[[email protected] www]# grep ‘195.154.216.165‘ 2015-11-28.access.log|wc -l289[[email protected]-dig www]#
 1 [[email protected] www]# curl ipinfo.io/195.154.216.165;echo‘‘ 2 { 3   "ip": "195.154.216.165", 4   "hostname": "fr.07.gs", 5   "city": "", 6   "region": "", 7   "country": "FR", 8   "loc": "48.8600,2.3500", 9   "org": "AS12876 ONLINE S.A.S."10 }11 [[email protected] www]# 

在 Nginx 上,所以可以使用 Nginx 的 Deny 來拒絕攻擊者的IP訪問。

 1 deny 195.154.211.220; 2 deny 195.154.188.28; 3 deny 195.154.188.186; 4 deny 180.97.106.161; 5 deny 180.97.106.162; 6 deny 180.97.106.36; 7 deny 195.154.180.69; 8 deny 195.154.211.26; 9 deny 221.229.166.247;10 deny 180.97.106.37;11 deny 195.154.216.164;12 deny 195.154.216.165;13 [[email protected] website]# 
通過代碼進行攔截過濾
 1 $config[‘request_limit‘][‘open‘] = true; // 開啟攔截請求 2 $config[‘request_limit‘][‘all_request‘] = false; // 攔截所有請求,否則攔截相同請求 3 $config[‘request_limit‘][‘min_request_interval_time‘] = 10;   // 允許的最小請求間隔 單位S(秒) 4 $config[‘request_limit‘][‘max_malicious_times‘] = 1;  // 允許的最大惡意請求次數 5  6 function maliciousRequestInterceptor() { 7     $config = array( 8         ‘all_request‘=> false,  9         ‘min_request_interval_time‘ => 1,10         ‘max_malicious_times‘ => 1011     );12     $request_limit_cfg = C("request_limit");13     if($request_limit_cfg && is_array($request_limit_cfg)){14         if($request_limit_cfg[‘open‘] == false){15             return true;16         }17         $config = array_merge($config,$request_limit_cfg);18     }19     session_start();20     $pre_request_url = $_SESSION[‘PRE_REQUEST_URL‘];21     $pre_request_time = $_SESSION[‘PRE_REQUEST_TIME‘];22     $cur_request_url = GetCurUrl();23     if(isset($pre_request_url) && isset($pre_request_time)){24         if((trim($pre_request_url) == trim($cur_request_url) || $config[‘all_request‘])25                 && (((microtime(TRUE) - floatval($pre_request_time))) < floatval($config[‘min_request_interval_time‘]))){26             $request_times = $_SESSION[‘MALICIOUS_REQUEST_TIMES‘];27             if(isset($request_times)){28                 $request_times = intval($request_times) + 1;29             }else{30                 $request_times = 1;31             }32             $_SESSION[‘MALICIOUS_REQUEST_TIMES‘] = $request_times;33             if ($request_times > $config[‘max_malicious_times‘]) {34 //                    error_log("intercept a malicious request ".$_SERVER[‘REMOTE_ADDR‘].":".$cur_request_url);35 //                    header($_SERVER["SERVER_PROTOCOL"]." 403 access denied."); 36 //                    exit();37                 return false;38             }39         }else{40             $_SESSION[‘MALICIOUS_REQUEST_TIMES‘] = 0;41         }42     }43     $_SESSION[‘PRE_REQUEST_URL‘] = $cur_request_url;44     $_SESSION[‘PRE_REQUEST_TIME‘] = microtime(TRUE);45     return true;46 }47 // php擷取當前訪問的完整url地址48 function GetCurUrl() {49     $url = ‘http://‘;50     if (isset ( $_SERVER [‘HTTPS‘] ) && $_SERVER [‘HTTPS‘] == ‘on‘) {51         $url = ‘https://‘;52     }53     if ($_SERVER [‘SERVER_PORT‘] != ‘80‘) {54         $url .= $_SERVER [‘HTTP_HOST‘] . ‘:‘ . $_SERVER [‘SERVER_PORT‘] . $_SERVER [‘REQUEST_URI‘];55     } else {56         $url .= $_SERVER [‘HTTP_HOST‘] . $_SERVER [‘REQUEST_URI‘];57     }58     return $url;59 }

 

PHP代碼,拒絕頻繁訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.