Typecho技巧:增加评论需要算术验证的小功能

引言

我想有的时候你会遇到这样的情况,打开自己的留言板,会发现很多莫名其妙的评论,当然,这里指的是垃圾评论。

这是不是让人很烦?

这篇文章就是提供一个方法,用来对付那些比较低级的垃圾评论——增加评论算术验证。

实现步骤

操作很简单,就是增加几行代码而已。

一、修改function.php

目前就博主使用的Cuteen主题为具体例子

进入目录~/themes/Cuteen/function.php

if (!defined('__TYPECHO_ROOT_DIR__')) exit;
require_once("Core/Cuteen.php");
require_once("Core/Content.php");
require_once("Core/ShortCodeUse.php");
function themeInit($self)
{
    Helper::options()->commentsAntiSpam = false; //关闭反垃圾
    Helper::options()->commentsCheckReferer = false; //关闭检查评论来源URL与文章链接是否一致判断(否则会无法评论)
    Helper::options()->commentsMaxNestingLevels = '999'; //最大嵌套层数
    Helper::options()->commentsPageDisplay = 'first'; //强制评论第一页
    Helper::options()->commentsOrder = 'DESC'; //将最新的评论展示在前
    Helper::options()->commentsHTMLTagAllowed = '<a href=""> <img src=""> <img src="" class=""> <code> <del>';
    Helper::options()->commentsMarkdown = true;
    Cuteen::upLike($self); //点赞
/* 
    $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;
}
*/  //该注释部分即将要添加的代码
    
    
error_reporting(0);
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Cuteen', 'parseContent');
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('Cuteen', 'parseContent');
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('Content', 'EditAddButton');
Typecho_Plugin::factory('admin/write-page.php')->bottom = array('Content', 'EditAddButton');

看到上方代码的注释部分了吗?

这就是我们即将添加的代码,但是,这是针对Cuteen主题的。

其它主题请使用下方代码:

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;
}

上方是50以内的数相加,如果觉得很难的话~可以将其更改为你自己所需要的数值。

二、修改comments.php

进入对应的目录~/themes/Cuteen/Base/comments.php

添加以下代码:

<?php spam_protection_math();?>

将其插入适当的位置,但什么是适当的位置呢?我将自己的代码给读者参考一下。

建议将其放到三项基本信息之后,当然,这只是建议,想怎么,请随意。

三、针对Cuteen主题的css优化

进入主题——设置——高级设置——自定义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;
}

结语

这个功能的实现并不是很复杂,但是不得不说它却能起到不错的效果。

如果想了解更多关于Cuteen主题的美化教程,可以看看这篇教程——Typecho-Cuteen主题的小小美化

如果有什么疑惑,可以在勿埋我心评论区留言。

参考资料:迷你日志 | 猫猫の窝

无标签
打赏
评论区
头像
    头像

    在前端(F12)把num元素删掉,就不用输入验证码也可以评论了............

      头像
      @SuiNian's Blog

      哈~是的,这个验证很简单。当初搞这个是因为被人恶意刷评论了,弄个小验证码就能比较有效的防止这些无聊脚本。

    头像
    凡涛
      

    我添加后,邮箱推送就用不了了

      头像
      @凡涛

      是不是你添加的位置不太恰当?

    头像
    文欢
      

    不管用噢,不输入也一样发表

      头像
      @文欢

      我刚刚试了没问题,如果不输入结果或者输入错了,就会显示“评论失败,回传参数错误”。
      你不输入就能在我的评论区直接评论吗?

        头像
        文欢
          
        @勿埋我心

        你的可以,我的加入就不管用了

          头像
          @文欢

          应该放在“昵称”、“邮箱”、“网址”的区块

          头像
          @文欢

          你应该是在comments.php中的位置选的不恰当

            头像
            文欢
              
            @勿埋我心

            不是不显示输入框,而是没有用,没用,我就删了

              头像
              @文欢

              按理说,不会有问题。你再检查一下自己的,可能是忽略了什么细节。
              如果还是不管用,那很遗憾,只能另寻他法,或者用反垃圾邮箱插件。

    头像
    文欢
      

    哈哈😄

      头像
文章目录