wordpress防止cc攻击的方法

1、新建cc.php

<?php
//代理IP直接退出
empty($_SERVER['HTTP_VIA']) or exit('Access Denied');
//防止快速刷新
session_start();
$seconds = '3'; //时间段[秒]
$refresh = '8'; //刷新次数
//设置监控变量
$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){
  //跳转防cc页
  header(sprintf('Location:%s', '/cc.html'));
  exit('Access Denied');
 }
}else{
 $_SESSION['refresh_times'] = 0;
 $_SESSION['last_time'] = $cur_time;
}

2、新建跳转页面cc.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>防CC刷新攻击中,5秒后跳转首页</title>
<meta http-equiv="refresh" content="5;url=/" />
<style>*{box-sizing:inherit;}body{min-height:100vh;display:flex;justify-content:center;align-items:center;background:#151924;padding:0;margin:0;}.loading{--color:#F5F9FF;--duration:2000ms;font-family:Roboto,Arial;font-size:24px;position:relative;white-space:nowrap;user-select:none;color:var(--color);}.loading span{--x:0;--y:0;--move-y:0;--move-y-s:0;--delay:0ms;display:block;position:absolute;top:0;left:0;width:1px;text-indent:calc(var(--x) * -1);overflow:hidden;transform:translate(var(--x),var(--y));}.loading.start div{opacity:0;}.loading.start span{animation:move var(--duration) ease-in-out var(--delay);}@keyframes move{30%{transform:translate(var(--x),var(--move-y));}82%{transform:translate(var(--x),var(--move-y-s));}</style>
</head>
<body>
<div class="loading">防CC刷新攻击中,5秒后跳转首页</div><br>
    <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script>
    <script type="text/javascript">
$(document).ready(function(){let loading=$('.loading').wrapInner('<div></div>'),min=20,max=70,minMove=10,maxMove=20;startAnimation(loading);loading.on('animationend webkitAnimationEnd oAnimationEnd','span:last-child',e=>{startAnimation(loading)});function setCSSVars(elem,min,max,minMove,maxMove){let width=Math.ceil(elem.width()),text=elem.text();for(let i=1;i<width;i++){let num=Math.floor(Math.random()*(max-min+1))+min,numMove=Math.floor(Math.random()*(maxMove-minMove+1))+minMove,dir=(i%2==0)?1:-1,spanCurrent=elem.find('span:eq('+i+')'),span=spanCurrent.length?spanCurrent:$('<span />');span.css({'--x':i-1+'px','--move-y':num*dir+'px','--move-y-s':((i%2==0)?num*dir-numMove:num*dir+numMove)+'px','--delay':i*10+'ms'});if(!spanCurrent.length){elem.append(span.text(text))}}}function startAnimation(elem){elem.removeClass('start');setCSSVars(elem,min,max,minMove,maxMove);void elem[0].offsetWidth;elem.addClass('start')}});
</script>
</body>
</html>

3、最后用编辑器打开index.php把下面代码放到最顶部

require 'cc.php';
标签:

发表评论