Typecho怎么实现评论算术验证

PHPz
풀어 주다: 2020-09-05 09:54:38
원래의
1621명이 탐색했습니다.

Typecho实现评论算术验证的方法:首先打开【comments.php】文件;然后在适当位置插入代码【】即可。

Typecho怎么实现评论算术验证

Typecho怎么实现评论算术验证?

Typecho无插件实现评论算术验证

Typecho的垃圾评论还是比较多的,除了插件外,还可以通过PHP函数实现简单的算术验证码。

第一步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[&#39;sum&#39;];
    switch($sum){
        case $_POST[&#39;num1&#39;]+$_POST[&#39;num2&#39;]:
        break;
        case null:
        throw new Typecho_Widget_Exception(_t(&#39;对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>&#39;,&#39;评论失败&#39;));
        break;
        default:
        throw new Typecho_Widget_Exception(_t(&#39;对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。&#39;,&#39;评论失败&#39;));
    }
    return $comment;
}
로그인 후 복사

第二步comments.php添加函数

打开主题comments.php文件,在适当位置插入如下代码:

<?php spam_protection_math();?>
로그인 후 복사

如果觉得100以内太难了,请修复function.php中添加的代码中rand后面的数字范围。

更多相关技术文章,请访问PHP中文网

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!