niihost

为 WordPress 搜索添加人机验证

WordPress内置的搜索是一个很占内存的功能,如果你的文章很多,那么执行一次搜索会相对卡顿,那么我们如何尽可能的防范一下呢?比如机器人扫描到了搜索页面,那将可能直接导致内存爆满mysql进程被终止。

我们可以加一个简单的搜索验证机制,用户在第一次搜索时需要进行简单的人机验证。一来这样可以有效防止恶意扫描导致内存崩溃,二来可以防止恶意请求关键字生成结果页面。

可将下面代码加到主题的functions.php里即可:

function esc_search_captcha( $query, $error = true ) {
	if ( is_search() && !is_admin() ) {
		if ( ! isset( $_COOKIE['esc_search_captcha'] ) ) {
			$query->is_search = false;
			$query->query_vars['s'] = false;
			$query->query['s'] = false;
 
			if ( $error == true ){
				//$query->is_404 = true;
				if ( isset( $_POST['result'] ) ) {
					if ( $_POST['result'] == $_COOKIE['result'] ) {
						$_COOKIE['esc_search_captcha'] = 1;
						setcookie('esc_search_captcha',1,0,'/');
						echo '<script>location.reload();</script>';
					}
				}
 
				$num1 = rand(1,50);
				$num2 = rand(1,50);
				$result = $num1+$num2;
				$_COOKIE['result'] = $result;
				setcookie('result',urldecode($result),0,'/');
				?>
 
				<html>
				<head>
				<meta charset="UTF-8">
				<title>人机验证</title>
				<style>
				body{color: #333;text-align: center;font-size: 16px;}
				.erphp-search-captcha{margin: 50px auto 15px;max-width: 250px;width: 100%;padding: 40px 20px;border: 1px solid #ddd;text-align: center;border-radius: 5px;}
				.erphp-search-captcha form{margin: 0}
				.erphp-search-captcha input{border: none;border-bottom: 1px solid #666;width: 50px;text-align: center;font-size: 16px;}
				.erphp-search-captcha input:focus{outline: none;}
				.erphp-search-captcha button{border: none;background: transparent;color: #ff5f33;cursor: pointer;}
				.erphp-search-captcha button:focus{outline: none;}
				a{color: #000;font-size: 12px;}
				</style>
				</head>
				<body>
				<div class="erphp-search-captcha">
				<form action="" method="post"><?php echo $num1;?> + <?php echo $num2;?> = <input type="text" name="result" required /> <button type="submit">验证</button></form>
				</div>
				<a href="<?php echo home_url();?>">返回首页</a>
				</body>
				</html>
				<?php
				exit;
			}
		}
	}
}
add_action( 'parse_query', 'esc_search_captcha' );

如果没有使用第三方的搜索,为WP默认搜索加个验证,还是非常必要的,效果可以看本站搜索。验证过一次后,只有关闭浏览器或者清空浏览器 cookie 才会需要再次验证。

美化版:

//搜索人机验证
function esc_search_captcha( $query, $error = true ) {
if ( is_search() && !is_admin() ) {
if ( ! isset( $_COOKIE['esc_search_captcha'] ) ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;

if ( $error == true ){
//$query->is_404 = true;
if ( isset( $_POST['result'] ) ) {
if ( $_POST['result'] == $_COOKIE['result'] ) {
$_COOKIE['esc_search_captcha'] = 1;
setcookie('esc_search_captcha',1,0,'/');
echo '<script>location.reload();</script>';
}
}

$num1 = rand(1,50);
$num2 = rand(1,50);
$result = $num1+$num2;
$_COOKIE['result'] = $result;
setcookie('result',urldecode($result),0,'/');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>人机验证</title>
<style>
body {
background-color: #f5f5f5;
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
text-align: center;
line-height: 1.6;
}
.container {
margin: 50px auto 15px;
max-width: 400px;
padding: 40px 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
background-color: #fff;
}
h1 {
font-size: 24px;
font-weight: normal;
margin-top: 0;
}
form {
margin: 0;
}
.input-group {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.label {
flex: 0 0 80px;
text-align: right;
margin-right: 20px;
}
.input {
flex: 1;
height: 40px;
border: none;
border-bottom: 1px solid #ccc;
padding: 0 10px;
font-size: 16px;
box-sizing: border-box;
}
.btn {
display: block;
width: 100%;
height: 40px;
line-height: 40px;
background-color: #ff5f33;
color: #fff;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.btn:hover {
background-color: #ff7854;
}
.btn:focus {
outline: none;
}
a {
color: #666;
font-size: 14px;
text-decoration: none;
}
a:hover {
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h1>人机验证</h1>
<form action="" method="post">
<div class="input-group">
<label class="label"><?php echo $num1;?> + <?php echo $num2;?> =</label>
<input type="text" name="result" class="input" required />
</div>
<button type="submit" class="btn">验证</button>
</form>
</div>
<a href="<?php echo home_url();?>" class="back-link">返回首页</a>
</body>
</html>
<?php
exit;
}
}
}
}
add_action( 'parse_query', 'esc_search_captcha' );
//搜索人机验证
function esc_search_captcha( $query, $error = true ) {
if ( is_search() && !is_admin() ) {
if ( ! isset( $_COOKIE['esc_search_captcha'] ) ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;

if ( $error == true ){
//$query->is_404 = true;
if ( isset( $_POST['result'] ) ) {
if ( $_POST['result'] == $_COOKIE['result'] ) {
$_COOKIE['esc_search_captcha'] = 1;
setcookie('esc_search_captcha',1,0,'/');
echo '<script>location.reload();</script>';
}
}

$num1 = rand(1,50);
$num2 = rand(1,50);
$result = $num1+$num2;
$_COOKIE['result'] = $result;
setcookie('result',urldecode($result),0,'/');
?>

<html>
<head>
<meta charset="UTF-8">
<title>人机验证</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
}
.erphp-search-captcha {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 350px;
width: 100%;
padding: 30px;
border-radius: 5px;
background-color: #f8f8f8;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
font-size: 18px;
line-height: 1.5;
text-align: center;
}
.erphp-search-captcha form {
margin-top: 20px;
}
.erphp-search-captcha input {
display: inline-block;
width: 60px;
height: 40px;
margin-right: 10px;
font-size: 20px;
text-align: center;
border: 2px solid #ddd;
border-radius: 3px;
transition: border-color 0.2s;
}
.erphp-search-captcha input:focus {
outline: none;
border-color: #ff5f33;
}
.erphp-search-captcha button {
display: inline-block;
padding: 0 20px;
height: 40px;
line-height: 40px;
font-size: 18px;
font-weight: bold;
color: #fff;
background-color: #ff5f33;
border: none;
border-radius: 3px;
cursor: pointer;
transition: background-color 0.2s;
}
.erphp-search-captcha button:hover {
background-color: #d44c2e;
}
.erphp-search-captcha button:focus {
outline: none;
}
.erphp-search-captcha a {
display: block;
margin-top: 20px;
color: #666;
font-size: 14px;
text-decoration: none;
transition: color 0.2s;
}
.erphp-search-captcha a:hover {
color: #333;
}
</style>
</head>
<body>
<div class="erphp-search-captcha">
<p>为了防止搜索机器人,需要进行人机验证。</p>
<form action="" method="post"><?php echo $num1;?> + <?php echo $num2;?> = <input type="text" name="result" required /> <button type="submit">验证</button></form>
</div>
<a href="<?php echo home_url();?>">返回首页</a>
</body>
</html>
<?php
exit;
}
}
}
}
add_action( 'parse_query', 'esc_search_captcha' );

给TA赏糖
共{{data.count}}人
人已赏糖
技术分享

微信内测版新增功能揭秘及使用攻略详解

2024-12-2 12:12:24

技术分享

无线网络连接上但上不了网如何排查解决问题步骤详解

2024-12-2 20:09:31

0 条回复 A文章作者 M管理员
技术宅评论
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索