wordpress防止网站被镜像四个方法
第一种:拆分域名链接与镜像站比对,然后用img标签src空值触发onerror来执行js比对,比对失败则跳转回源站。代码如下:(复制粘贴到主题的functions.php最后一个?>之前,代码出现的域名换成自己的域名)
add_action('wp_footer','deny_mirrored_websites'); function deny_mirrored_websites(){ $currentDomain = 'baidu." + "com'; //此处自行拆分一下自己的域名即可 echo '<img style="display:none" src=" " onerror=\'var str1="'.$currentDomain.'";str2="docu"+"ment.loca"+"tion.host";str3=eval(str2);if( str1!=str3 ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + "\"' . $currentDomain .'\"" + ")";eval(do_action) }\' />'; }
第二种:html静态版本,是上面的变种,只是把上面卸载js中的用写在静态代码里了。将代码中出现的域名换成你自己的域名
<img style="display:none" src=" " onerror='var currentDomain="baidu." + "com"; var str1=currentDomain; str2="docu"+"ment.loca"+"tion.host"; str3=eval(str2) ;if( str1!=str3 ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + "currentDomain" + ")";eval(do_action) }' />
第三种:通过php判断UA为空或含有PHP的请求,否则弹出提示并禁止打开
/** * WordPress 禁止UA为空或含有PHP的请求 **/ if(!is_admin()) { add_action('init', 'deny_mirrored_request', 0); } function deny_mirrored_request() { $ua = $_SERVER['HTTP_USER_AGENT']; if(!$ua || preg_match('/PHP/i', $ua)) { header("Content-type: text/html; charset=utf-8"); wp_die('请勿采集本站,因为采集的站长木有小JJ!'); } }
第四种:判断更多请求样式
add_action('wp_head', 'deny_mirrored_request', 0); function deny_mirrored_request() { $ua = $_SERVER['HTTP_USER_AGENT']; $now_ua = array('FeedDemon ','BOT/0.1 (BOT for JCE)','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','lightDeckReports Bot','PHP'); if(!$ua) { header("Content-type: text/html; charset=utf-8"); wp_die('请勿采集本站,采集者木有小JJ!'); }else{ foreach($now_ua as $value ) if(eregi($value,$ua)) { header("Content-type: text/html; charset=utf-8"); wp_die('请勿采集本站,采集者木有小JJ!'); } } }