PHP怎么实现cc攻击,PHP防CC攻击实现代码总结_PHP教程

/*

* 防CC攻击郁闷到死,不死版。

*

* 如果每秒内网站刷新次数超过2次,延迟5秒后访问。

*/

$cc_min_nums = ‘1’; //次,刷新次数

$cc_url_time = ‘5’; //秒,延迟时间

//$cc_log = ‘cc_log.txt’; //启用本行为记录日志

$cc_forward = ‘http://localhost’; //释放到URL

//——————————————–

//返回URL

$cc_uri = $_SERVER[‘REQUEST_URI’]?$_SERVER[‘REQUEST_URI’]:($_SERVER[‘PHP_SELF’]?$_SERVER[‘PHP_SELF’]:$_SERVER[‘SCRIPT_NAME’]);

$site_url = ‘http://’.$_SERVER [‘HTTP_HOST’].$cc_uri;

//启用session

if( !isset( $_SESSION ) ) session_start();

$_SESSION[“visiter”] = true;

if ($_SESSION[“visiter”] true){

echo “setTimeout(“window.location.href =’$cc_forward’;”, 1);”;

//header(“Location: “.$cc_forward);

exit;

}

$timestamp = time();

$cc_nowtime = $timestamp ;

if (session_is_registered(‘cc_lasttime’)){

$cc_lasttime = $_SESSION[‘cc_lasttime’];

$cc_times = $_SESSION[‘cc_times’] + 1;

$_SESSION[‘cc_times’] = $cc_times;

}else{

$cc_lasttime = $cc_nowtime;

$cc_times = 1;

$_SESSION[‘cc_times’] = $cc_times;

$_SESSION[‘cc_lasttime’] = $cc_lasttime;

}

//获取真实IP

if (isset($_SERVER)){

$real_ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];

}else{

$real_ip = getenv(“HTTP_X_FORWARDED_FOR”);

}

//print_r($_SESSION);

//释放IP

if (($cc_nowtime – $cc_lasttime)<=0){

if ($cc_times>=$cc_min_nums){

if(!empty($cc_log)) cc_log(get_ip(), $real_ip, $cc_log, $cc_uri); //产生log

echo “Wait please, try again later!setTimeout(“window.location.href =’$site_url’;”, 5000);”;

//printf(‘您的刷新过快,请稍后。’);

//header(“Location: “.$cc_forward);

exit;

}

}else{

$cc_times = 0;

$_SESSION[‘cc_lasttime’] = $cc_nowtime;

$_SESSION[‘cc_times’] = $cc_times;

}=0){

//记录cc日志

function cc_log($client_ip, $real_ip, $cc_log, $cc_uri){

$temp_time = date(“Y-m-d H:i:s”, time() + 3600*8);

$temp_result = “[“.$temp_time.”] [client “.$client_ip.”] “;

if($real_ip) $temp_result .= ” [real “.$real_ip.”] “;

$temp_result .= $cc_uri . “rn”;

$handle = fopen (“$cc_log”, “rb”);

$oldcontent = fread($handle,filesize(“$cc_log”));

fclose($handle);

$newcontent = $temp_result . $oldcontent;

$fhandle=fopen(“$cc_log”, “wb”);

fwrite($fhandle,$newcontent,strlen($newcontent));

fclose($fhandle);

}

//获取在线IP

function get_ip() {

global $_C;

if(empty($_C[‘client_ip’])) {

if(getenv(‘HTTP_CLIENT_IP’) && strcasecmp(getenv(‘HTTP_CLIENT_IP’), ‘unknown’)) {

$client_ip = getenv(‘HTTP_CLIENT_IP’);

} elseif(getenv(‘HTTP_X_FORWARDED_FOR’) && strcasecmp(getenv(‘HTTP_X_FORWARDED_FOR’), ‘unknown’)) {

$client_ip = getenv(‘HTTP_X_FORWARDED_FOR’);

} elseif(getenv(‘REMOTE_ADDR’) && strcasecmp(getenv(‘REMOTE_ADDR’), ‘unknown’)) {

$client_ip = getenv(‘REMOTE_ADDR’);

} elseif(isset($_SERVER[‘REMOTE_ADDR’]) && $_SERVER[‘REMOTE_ADDR’] && strcasecmp($_SERVER[‘REMOTE_ADDR’], ‘unknown’)) {

$client_ip = $_SERVER[‘REMOTE_ADDR’];

}

$_C[‘client_ip’] = $client_ip ? $client_ip : ‘unknown’;

}

return $_C[‘client_ip’];

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值