通過在tp5項目中添加以下代碼即可實現預防cc攻擊的功能。
//防cc攻擊
sheli_cc();
function sheli_cc(){
//代理IP直接退出
empty($_SERVER['HTTP_VIA']) or exit('Access Denied');
//防止快速刷新
session_start();
$seconds = '60'; //時間段[秒]
$refresh = '12'; //刷新次數
//設置監控變量
$cur_time = time();
if(isset($_SESSION['last_time'])){
$_SESSION['refresh_times'] += 1;
}else{
$_SESSION['refresh_times'] = 1;
$_SESSION['last_time'] = $cur_time;
}
//處理監控結果
if($cur_time - $_SESSION['last_time'] < $seconds){
if($_SESSION['refresh_times'] >= $refresh){
//跳轉至攻擊者服務器地址
//header(sprintf('Location:%s', 'http://127.0.0.1'));
exit('請求頻率太快,稍候'.$seconds.'秒后再訪問!');
}
}else{
$_SESSION['refresh_times'] = 0;
$_SESSION['last_time'] = $cur_time;
}
}
?>