星空娱乐

优势:简单易用,无论你懂不懂PHP开发,只要会复制粘贴就行。Memcached内存缓存技术,高性能,高并发。精准无误,0误封。能防护QPS10000以内的CC攻击,看机器配置,有可能更高。支持范围较广,直接你网站支持PHP就行。源码简单,可自行修改,可以套自己喜欢的前端模板主题。要求:PHP版本≥7.2PHP需安装Memcached扩展Memcached≥1.6使用方法以下开源代码添加到你网站核心文件中,相当于你网站任何页面都会引用的一个文件。类似于:config.phpWordPress网站添加到根目录的/index.php头部即可。其他PHP网站根据你的程序逻辑决定。开源代码<?phpini_set("display_errors","Off");error_reporting(E_ALL^E_NOTICE^E_WARNING);extension_loaded('memcached')ordie('memcached扩展未安装!');$logPath=$_SERVER['DOCUMENT_ROOT'].'/waf/waf.log';//日志记录文件保存路径,$_SERVER['DOCUMENT_ROOT']是网站根目录$fileht=$_SERVER['DOCUMENT_ROOT'].'/waf/ban.log';//被拉黑IP记录文件保存路径if(!file_exists($logPath)){@mkdir($_SERVER['DOCUMENT_ROOT'].'/waf/',0777,true);@file_put_contents($logPath,'');@file_put_contents($fileht,'');}$allowtime=2;//防刷新时间(秒)$allownum=5;//防刷新次数(比如2秒5次,超过就警告)$allowRefresh=10;//在此警告次数之后拉黑IP$bantime=600;//封禁时间,超时自动解封(秒)$ip=$_SERVER['HTTP_X_FORWARDED_FOR']?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];$uri=$_SERVER['PHP_SELF'];$cache=newMemcached();$cache->addServer('127.0.0.1','11211')ordie('memcached连接失败!');$inban=$cache->get('waf-ban-'.$ip);if($inban){header("HTTP/1.1403Forbidden");exit('<h1>403Forbidden非法访问</h1><p>你的请求异常,已被服务器防火墙拦截,</br>如果你在开发测试过程中超频被封IP,请等待'.($bantime/60).'分钟后自动解封</br>你的IP:'.$ip.'</p>');}$wafarr=$cache->get('waf-'.$ip);if(!$wafarr){$wafarr=['path'=>$uri,'time'=>time()+$allowtime,'sum'=>1,];$cache->set('waf-'.$ip,$wafarr,time()+$allowtime);}else{if($wafarr['sum']>$allownum){$wafsum_arr=$cache->get('waf-sum-'.$ip);if(!$wafsum_arr){$wafsum_arr=['sum'=>1,];$cache->set('waf-sum-'.$ip,$wafsum_arr,time()+$bantime);}else{if($wafsum_arr['sum']>$allowRefresh){$cache->set('waf-ban-'.$ip,1,time()+$bantime);file_put_contents($fileht,$ip."\n",FILE_APPEND);}else{$wafsum_arr['sum']++;$cache->set('waf-sum-'.$ip,$wafsum_arr,time()+$bantime);}}file_put_contents($logPath,$ip.'--'.date('Y-m-dH:i:s',time()).'--'.$uri."\n",FILE_APPEND);header("HTTP/1.1403Forbidden");exit("请求频率QPS超过限制,请酌情访问,多次提醒后会封禁IP!");}else{$wafarr['sum']++;$cache->set('waf-'.$ip,$wafarr,$wafarr['time']);}}