资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!
您需要 登录 才可以下载或查看,没有账号?立即注册
×
可设置几分钟(可以自己改代码为秒)内搜索几次时,自动封禁某IP的搜索功能多少秒,建议与封禁时间与时间窗口时长一致
避免WordPress搜索功能被刷,导致服务器CPU使用率和负载状态100%爆满
以下代码放入主题函数function.php中,子比放入func.php中
class WXS_SearchFrequencyLimit {
// 配置参数可根据需要修改
private $search_limit_count = 3; // 允许的最大搜索次数
private $time_window_in_minutes = 1; // 时间窗口(分钟)
private $block_time_in_seconds = 10; // 超过限制后封禁时间(秒)
private $block_message = '您的搜索频率过高,请在 %d 秒后再试。'; // 封禁提示消息
public function __construct() {
add_filter( 'pre_get_posts', array( $this, 'check_search_limit' ) );
if ( ! is_admin() ) {
add_action( 'wp_loaded', array( $this, 'check_block_status_on_any_page' ) );
}
}
private function get_visitor_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return sanitize_text_field( $ip );
}
private function set_block_cache($key, $expiration) {
$expire_time = time() + $expiration;
wp_cache_set($key, $expire_time, 'search_limit', $expiration);
}
private function get_remaining_block_time($key) {
$expire_time = wp_cache_get($key, 'search_limit');
if ($expire_time === false) {
return 0;
}
$remaining = $expire_time - time();
return $remaining > 0 ? $remaining : 0;
}
public function check_block_status_on_any_page() {
$visitor_ip = $this->get_visitor_ip();
$cache_key = 'search_block_' . $visitor_ip;
$remaining_time = $this->get_remaining_block_time($cache_key);
if ($remaining_time > 0 && !is_search()) {
$message = sprintf($this->block_message, $remaining_time);
add_action('wp_body_open', function() use ($message) {
echo '<div style="background-color: #fff3cd; color: #856404; padding: 15px; text-align: center; border: 1px solid #ffeeba; margin-bottom: 20px;">';
echo esc_html($message);
echo '</div>';
});
}
}
public function check_search_limit($query) {
if (!is_admin() && $query->is_search && $query->is_main_query()) {
$visitor_ip = $this->get_visitor_ip();
$limit = $this->search_limit_count;
$window_seconds = $this->time_window_in_minutes * 60;
$block_time = $this->block_time_in_seconds;
$cache_key = 'search_block_' . $visitor_ip;
$records_key = 'search_records_' . $visitor_ip;
$remaining_time = $this->get_remaining_block_time($cache_key);
if ($remaining_time > 0) {
wp_die(
sprintf($this->block_message, $remaining_time),
'搜索频率限制', //标题
array('response' => 429)
);
}
$search_records = wp_cache_get($records_key, 'search_limit', false, $found);
if (!$found) {
$search_records = array();
}
$current_time = time();
$search_records = array_filter($search_records, function($timestamp) use ($current_time, $window_seconds) {
return $current_time - $timestamp < $window_seconds;
});
if (count($search_records) >= $limit) {
$this->set_block_cache($cache_key, $block_time);
wp_cache_delete($records_key, 'search_limit');
wp_die(
sprintf($this->block_message, $block_time),
'搜索频率限制',
array('response' => 429)
);
} else {
$search_records[] = $current_time;
wp_cache_set($records_key, $search_records, 'search_limit', $window_seconds);
}
}
return $query;
}
}
new WXS_SearchFrequencyLimit();
其次,你可以使用WAF对/?s=限制 |