WordPress防采集插件怎么写 PRO版 防止火车头采集
<?php
/*
* Plugin Name: Anti-Robot
* Plugin URI: https://www.ggplus.cn/
* Description: A plugin to prevent robots from scraping your site
* Author: GGplus
* Version: 1.0
* License: GPL2+
*/
function anti_robot_footer() {
if ( ! is_user_logged_in() ) {
$random_string = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5);
echo '<input type="hidden" name="anti_robot" value="' . $random_string . '">';
}
}
add_action( 'wp_footer', 'anti_robot_footer' );
function anti_robot_check() {
if ( ! is_user_logged_in() && ! isset( $_POST['anti_robot'] ) ) {
wp_die( 'Sorry, robots are not allowed.' );
}
}
add_action( 'init', 'anti_robot_check' );
该插件在页面底部添加一个隐藏的输入框,在每次请求时都会检查该输入框是否存在。如果该输入框不存在(即不是用户手动提交的请求,而是被火车头采集器提交的请求),则会终止请求。
插件里面的
$random_string = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5);
是随机生成字符串, 用来做校验.
请注意,这只是一个示例,实际使用中可能需要进行更多的调整和优化。
请注意:加入以上代码,火车头采集时出现500,但是当火车头采集器加入网站的cookie后,又可以正常采集了。并不能完全防止火车头采集。
当火车头采集器模拟了一个用户并在请求中提供了合法的 cookie 时,这个插件就无法阻止火车头采集器采集网站的内容了。
一种可行的方法是使用一些更高级的防护技术,例如:
验证 IP 地址:当请求来自非法 IP 地址时,可以拒绝该请求。
验证 User-Agent:如果请求中的 User-Agent 不是合法的浏览器,也可以拒绝该请求。
验证请求频率:如果请求的频率过高,可以暂时阻止该 IP 地址的请求。
CAPTCHA 验证:在某些请求中强制用户输入验证码,可以有效阻止机器人自动提交表单。
这些方法都需要在服务器端进行验证。如果你是没有服务器经验的话,那可能需要求助于专业人士。
另一种解决方案,就是使用更为先进的防采集产品,可以帮助你来防止被采集,有些产品可能提供更先进的防采集技术,比如反恶意采集,防爬虫,反爬虫,反采集等.