这几天垃圾评论太多了,所以想解决一下,就想起了之前看到的一篇文章,添加验证码,说干就干。
第一步function.php如下函数
function themeInit($comment){
$comment = spam_protection_pre($comment, $post, $result);
}
function spam_protection_math(){
$num1=rand(1,49);
$num2=rand(1,49);
echo "<label for=\"math\">请输入<code>$num1</code>+<code>$num2</code>的计算结果:</label>\n";
echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" style=\"width:218px\" placeholder=\"计算结果:\">\n";
echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
}
function spam_protection_pre($comment, $post, $result){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:
break;
case null:
throw new Typecho_Widget_Exception(_t('对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>','评论失败'));
break;
default:
throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。','评论失败'));
}
return $comment;
}
因为我使用的主题function.php里面已经有了themeInit这个函数,所以按照原博主的方法,掐头去尾后得到
$comment = spam_protection_pre($comment, $post, $result);
}
function spam_protection_math(){
$num1=rand(1,49);
$num2=rand(1,49);
echo "<label for=\"math\">请输入<code>$num1</code>+<code>$num2</code>的计算结果:</label>\n";
echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" style=\"width:218px\" placeholder=\"计算结果:\">\n";
echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
}
function spam_protection_pre($comment, $post, $result){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:
break;
case null:
throw new Typecho_Widget_Exception(_t('对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>','评论失败'));
break;
default:
throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。','评论失败'));
}
return $comment;
第二步comments.php添加函数
打开主题comments.php文件,在适当为止插入如下代码:
<?php spam_protection_math();?>
我这里加在了填写基础信息后面,如果觉得100以内太难了,请修改function.php中添加的代码中rand后面的数字范围。
最后
因为原始的样式有点丑,所以我做了点修改,下面放出我修改后的,适用本主题,其他主题请看上面那一条
$comment = spam_protection_pre($comment, $post, $result);
}
function spam_protection_math(){
$num1=rand(1,10);
$num2=rand(1,10);
echo "<input class='input mr1' id='jisuan' type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" placeholder=\"$num1+$num2=?\">\n";
echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
}
function spam_protection_pre($comment, $post, $result){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:
break;
case null:
throw new Typecho_Widget_Exception(_t('对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>','评论失败'));
break;
default:
throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。','评论失败'));
}
return $comment;
修改后后,到 主题设置-高级设置-自定义css处,填写以下样式:
.jisuan {
font-family: inherit;
font-size: inherit;
display: block;
border: 3px solid rgba(26, 188, 156, 0.4);
border-radius: var(--yj);
box-sizing: border-box;
padding: 0px 8px;
}
input#jisuan {
margin-left: 10px;
width: 40%;
}
input#author,input#mail,input#url {
margin-left: 10px;
}
参考资料:
好烦 我也要搞起来
我也用了这个验证,还是有群发机能突破,但是比不加好多了
加上这个到现在7天过去了,还没遇到,比不加好太多了
针不戳,这猫针不戳
嘿嘿
感谢大佬,这个好,我天天饱受垃圾评论的骚扰,这下可以清静清静了!![]()
加上之后清净多了
我是不知道招惹谁了两三天来了接近300条空评论...全在一篇文章下面,所以直接把那篇文章禁止评论了
300多条
应该是故意的的了
最后还是被迫加上了...垃圾评论蔓延到别的文章下面了
我的破站最近也有好多这种垃圾评论,一开始只是同一个IP,屏蔽了之后各种IP都有了 ,最后懒得折腾直接禁用了非中文语系的评论,现在总算是没有了..
![]()
PS:新加的验证码手机上面看排版乱了哎
主要是我那些垃圾评论里还有中文,ip还是国内的,就很烦
我那里甚至有卖片哥的评论
,现在开了个插件让百度内容审核帮我过滤掉敏感的评论内容
应该行了,换了个位置