WordPress教程 · WPtech

WordPress簡單的防止CC(5秒盾)PHP通用

小编 · 6月28日 · 2020年

前言

利用檢測cookie是否為正常的值,來確定是否為正常用戶訪問。PHP寫的所以如果攻擊的人代理很多的話,效果還是不大的!所以!做人要低調!

WordPress簡單的防止CC(5秒盾)PHP通用

代码

建立個cc.php文件

<?的PHP
定義('SYSTEM_ROOT' ,目錄名(preg_replace ('@ \\(。* \\(。* $ @' ,'' ,preg_replace ('@ \\(。* \\(。* $ @' ,`` ,__FILE__ )))。'/' );    
session_start ();
date_default_timezone_set ('亞洲/上海' );
標頭('Content-Type:text / html; charset = UTF-8' );
函數getspider ($ useragent = '' ){
	如果(CC_Defender == 2 )返回false ; 
	if (!$ useragent ){ $ useragent = $ _SERVER [ 'HTTP_USER_AGENT' ];}
$ useragent = strtolower ($ useragent );
	if (strpos ($ useragent ,'baiduspider' )!== false ){ 返回'baiduspider' ;}     
	if (strpos ($ useragent ,'googlebot' )!== false ){ 返回'googlebot' ;}     
	if (strpos ($ useragent ,'soso' )!== false ){ 返回'soso' ;}     
	if (strpos ($ useragent ,'bing' )!== false ){ 返回'bing' ;}     
	if (strpos ($ useragent ,'yahoo' )!== false ){ 返回'yahoo' ;}     
	if (strpos ($ useragent ,'sohu-search' )!== false ){ 返回'Sohubot' ;}     
	if (strpos ($ useragent ,'sogou' )!== false ){ 返回'sogou' ;}     
	if (strpos ($ useragent ,'youdaobot' )!== false ){ 返回'YoudaoBot' ;}     
	if (strpos ($ useragent ,'yodaobot' )!== false ){ 返回'YodaoBot' ;}     
	if (strpos ($ useragent ,'robozilla' )!== false ){ 返回'Robozilla' ;}     
	if (strpos ($ useragent ,'msnbot' )!== false ){ 返回'msnbot' ;}     
	if (strpos ($ useragent ,'lycos' )!== false ){ 返回'Lycos' ;}     
	if (strpos ($ useragent ,'ia_archiver' )!== false || strpos ($ useragent ,'iaarchiver' )!== false ){ 返回'alexa' ;}         
	if (strpos ($ useragent ,'archive.org_bot' )!== false ){ 返回'Archive' ;}      
	if (strpos ($ useragent ,'robozilla' )!== false ){ 返回'Robozilla' ;}      
	if (strpos ($ useragent ,'sitebot' )!== false ){ 返回'SiteBot' ;}      
	if (strpos ($ useragent ,'mj12bot' )!== false ){ 返回'MJ12bot' ;}      
	if (strpos ($ useragent ,'gosospider' )!== false ){ 返回'gosospider' ;}      
	if (strpos ($ useragent ,'gigabot' )!== false ){ 返回'Gigabot' ;}      
	if (strpos ($ useragent ,'yrspider' )!== false ){ 返回'YRSpider' ;}      
	if (strpos ($ useragent ,'gigabot' )!== false ){ 返回'Gigabot' ;}      
	if (strpos ($ useragent ,'jikespider' )!== false ){ 返回'jikespider' ;}      
	if (strpos ($ useragent ,'addsugarspiderbot' )!== false ){ 返回'AddSugarSpiderBot' ; / *非常少* / }      
	if (strpos ($ useragent ,'testspider' )!== false ){ 返回'TestSpider' ;}      
	if (strpos ($ useragent ,'etaospider' )!== false ){ 返回'EtaoSpider' ;}      
	if (strpos ($ useragent ,'wangidspider' )!== false ){ 返回'WangIDSpider' ;}      
	if (strpos ($ useragent ,'foxspider' )!== false ){ 返回'FoxSpider' ;}      
	if (strpos ($ useragent ,'docomo' )!== false ){ 返回'DoCoMo' ;}      
	if (strpos ($ useragent ,'yandexbot' )!== false ){ 返回'YandexBot' ;}      
	if (strpos ($ useragent ,'ezooms' )!== false ){ 返回'Ezooms' ; / *個人* / }      
	if (strpos ($ useragent ,'sinaweibobot' )!== false ){ 返回'SinaWeiboBot' ;}      
	if (strpos ($ useragent ,'catchbot' )!== false ){ 返回'CatchBot' ;}      
	if (strpos ($ useragent ,'surveybot' )!== false ){ 返回'SurveyBot' ;}      
	if (strpos ($ useragent ,'dotbot' )!== false ){ 返回'DotBot' ;}      
	if (strpos ($ useragent ,'purebot' )!== false ){ 返回'Purebot' ;}      
	if (strpos ($ useragent ,'ccbot' )!== false ){ 返回'CCBot' ;}      
	if (strpos ($ useragent ,'mlbot' )!== false ){ 返回'MLBot' ;}      
	if (strpos ($ useragent ,'adsbot-google' )!== false ){ 返回'AdsBot-Google' ;}     
	if (strpos ($ useragent ,'ahrefsbot' )!== false ){ 返回'AhrefsBot' ;}     
	if (strpos ($ useragent ,'spbot' )!== false ){ 返回'spbot' ;}     
	if (strpos ($ useragent ,'augustbot' )!== false ){ 返回'AugustBot' ;}     
	返回false ; 
}
 
if ($ _GET [ 'rand' ] && $ _SESSION [ 'rand_session' ]!= $ _GET [ 'rand' ]){ 
	@header ('Content-Type:text / html; charset = UTF-8' );
exit ('<b>瀏覽器不支持COOKIE或不正常訪問!</ b>' );
}
if (!$ _SESSION [ 'rand_session' ] && $ nosecu != true ){ 
	如果(!getspider ()){
		$ rand_session = MD5 (uniqid 。()蘭特(1 ,1000 ));
	$ _SESSION [ 'rand_session' ] = $ rand_session ;
		退出(“ <!DOCTYPE HTML>
		<html>
		<頭>
		<meta charset = \“ UTF-8 \” />
		<meta name = \“ viewport \” content = \“ width = device-width,initial-scale = 1,maximum-scale = 1 \” />
 
		<title>安全檢查中... </ title>
	 <script> var i = 5; 
  var intervalid; 
  intervalid = setInterval(\“ fun()\”,1000); 
函數fun(){ 
如果(i == 0){ 
window.location.href = \“?{$ _ SERVER ['QUERY_STRING']}&rand = {$ rand_session} \”; 
clearInterval(intervalid); 
} 
document.getElementById(\“ mes \”)。innerHTML = i; 
一世 - ; 
} 
</ script> 
<樣式>
	html,正文{width:100%; 高度:100%; 邊距:0; 填充:0;}
    正文{background-color:#ffffff; 字體家族:Helvetica,Arial,無襯線;字體大小:100%;}
    h1 {font-size:1.5em; 顏色:#404040; 文字對齊:居中;}
    p {font-size:1em; 顏色:#404040; 文字對齊:居中;邊距:10px 0 0 0;}
    #spinner {保證金:0自動30px自動;顯示:阻止;}
    .attribution {margin-top:20px;}
  </ style>
  </ head>
<身體>
  <table width = \“ 100%\” height = \“ 100%\” cellpadding = \“ 20 \”>
    <tr>
      <td align = \“ center \” valign = \“ middle \”>
    <noscript> <h2>請打開瀏覽器的javascript,然後刷新瀏覽器</ h2> </ noscript>
  <h1> <span data-translate = \“ checking_browser \”>瀏覽器安全檢查中... </ span> </ h1>
    <p data-translate = \“ process_is_automatic \”> </ p>
    <p data-translate = \“ allow_5_secs \”>還剩<span id = \“ mes \”> 5 </ span>秒</ p>
  </ div>
</ div>
  </ td>
    </ tr>
</ table> </ body> </ html>“ );
}
}

調用:

WordPress調用,主題/header.php第一行添加代碼:

<?php get_template_part ('template / cc' ); ?>   

首次訪問網站檢測瀏覽器cookie通過才能正常的訪問您的網站,搜索引擎蜘蛛已經過濾,不影響收錄,還有未加上的可以自己補充。平時沒攻擊的時候建議不要開啟,影響網站體驗,配合Nginx lua waf簡單防禦CC 攻擊效果更佳。

0 条回应