コアリクエストのみを処理するスクリプトを作成しました(セッションリクエストやコアを呼び出さないその他のリクエストはありません)。あなたがグーグルを見るならば、あなたは毎回高い負荷のためにあなたのサーバーを殺すであろうスクリプト/クラスを見つけるでしょう。多くの人がSESSIONを使用しており、SQL / Databaseも使用すると、サーバーキラーとしてフラッディング保護を利用できるようになります。また、SESSIONにはCookie(またはGET SID)が必要であるため、SESSIONを簡単に操作して新しいSESSIONIDを取得できます。
私の関数はテキストベースで、簡単な処理を行います。悪いことは、時々CronJobを使用してipsを削除しなければならないことです。他のスクリプトと比較すると、約10 *高速です(セッションよりも節約できます)。
それが本当に役立つかどうかはわかりません。;)rpm値を以下または/および200reqに変更したい場合があります。私の設定では、ボットが6秒未満でインターバルリクエストを実行することを禁止しています。
<?php
function ht_request_limiter() {
if (!isset($_SERVER['REMOTE_ADDR'])) { return; } // Maybe its impossible, however we check it first
if (empty($_SERVER['REMOTE_ADDR'])) { return; } // Maybe its impossible, however we check it first
$path = '/your/path/ipsec/'; // I use a function to validate a path first and return if false...
$path = $path.$_SERVER['REMOTE_ADDR'].'.txt'; // Real file path (filename = <ip>.txt)
$now = time(); // Current timestamp
if (!file_exists($path)) { // If first request or new request after 1 hour / 24 hour ban, new file with <timestamp>|<counter>
if ($handle = fopen($path, 'w+')) {
if (fwrite($handle, $now.'|0')) { chmod($path, 0700); } // Chmod to prevent access via web
fclose($handle);
}
}
else if (($content = file_get_contents($path)) !== false) { // Load existing file
$content = explode('|',$content); // Create paraset [0] -> timestamp [1] -> counter
$diff = (int)$now-(int)$content[0]; // Time difference in seconds from first request to now
if ($content[1] == 'ban') { // If [1] = ban we check if it was less than 24 hours and die if so
if ($diff>86400) { unlink($path); } // 24 hours in seconds.. if more delete ip file
else {
header("HTTP/1.1 503 Service Unavailable");
exit("Your IP is banned for 24 hours, because of too many requests.");
}
}
else if ($diff>3600) { unlink($path); } // If first request was more than 1 hour, new ip file
else {
$current = ((int)$content[1])+1; // Counter + 1
if ($current>200) { // We check rpm (request per minute) after 200 request to get a good ~value
$rpm = ($current/($diff/60));
if ($rpm>10) { // If there was more than 10 rpm -> ban (if you have a request all 5 secs. you will be banned after ~17 minutes)
if ($handle = fopen($path, 'w+')) {
fwrite($handle, $content[0].'|ban');
fclose($handle);
// Maybe you like to log the ip once -> die after next request
}
return;
}
}
if ($handle = fopen($path, 'w+')) { // else write counter
fwrite($handle, $content[0].'|'.$current .'');
fclose($handle);
}
}
}
}
編集:リクエスト時間をテストする私の方法は、マイクロタイムを使用して、10,000人のユーザーをシミュレートすることでした。私はグーグルに尋ねてテストしました(例として)http://technitip.net/simple-php-flood-protection-class
だから私はそこで何が単純であるべきか分かりませんか?次のように、一度に約3つのSQLリクエストがあります。
$this -> user_in_db($ip))
$this->user_flooding($ip);
$this->remove_old_users();
より多くの機能を提供するかもしれませんが、すべての正当なユーザーはサーバータイムを無料で使用します。;)