已建站九年的老牌绿色软件站
不忘初心,坚持每日更新不易!

给WordPress网站搜索添加一个“人机验证”功能的美化版

这段代码的作用是在WordPress的搜索功能中添加人机验证,在用户进行搜索时,如果没有通过人机验证,则不允许进行搜索,需要重新输入正确的结果才能进行搜索。具体实现是通过拦截WordPress的查询操作,判断是否为搜索操作,如果是则进行人机验证。如果验证通过,则设置一个cookie,以后再进行搜索时就不需要再进行验证了。如果验证未通过,则输出人机验证的界面,要求用户输入正确的计算结果才能进行搜索。

第一种方式

WPCaptcha

将下面代码添加到主题函数模板 functions.php中:

/** 
* WordPress 纯代码添加搜索人机验证
* https://www.ypojie.com/12140.html 
*/
//搜索人机验证
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' );

第二种方式

WPCaptcha

将下面代码添加到主题函数模板 functions.php中:

/** 
* WordPress 纯代码添加搜索人机验证 
* https://www.ypojie.com/12140.html 
*/ 
//搜索人机验证
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' );

以上代码可实现搜索人机验证,但是存在一个问题,管理员搜索也会验证,我们需要添加一个权限判断,如果当前用户是管理员,则直接进行搜索,不进行人机验证

下面是完整代码,增加排除管理员,代码登陆可见

此内容查看价格为1积分,请先
由于采集站猖狂,部分内容需使用积分查看,每日签到可免费获取积分(点我查看)。

这段代码会在搜索时拦截查询操作,如果用户没有通过人机验证,则会输出美化过的人机验证界面,要求用户输入正确的计算结果才能进行搜索。界面使用了灰色背景和简洁的样式,更加美观大方。

赞(1006)

这些信息可能会帮助到你: 下载帮助 | 报毒说明 | 进站必看 | 关于我们

版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《给WordPress网站搜索添加一个“人机验证”功能的美化版》
文章链接:https://www.ypojie.com/12140.html
免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途,网站会员捐赠是您喜欢本站而产生的赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求。