typecho纯代码算术验证码
Typecho的垃圾评论还是比较多的,除了插件外,还可以通过PHP函数实现简单的算术验证码。
第一步function.php如下函数
//算术验证评论by minirizhi.com
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;
}
第二步comments.php添加函数
打开主题comments.php文件,在适当为止插入如下代码:
<?php spam_protection_math();?>
如果觉得100以内太难了,请修复function.php中添加的代码中rand后面的数字范围。
- 上一篇: typecho纯代码输出文章缩略图
- 下一篇: 《失眠》





简单实用
谢谢分享
用了增强版之后
好像已登录的用户,如管理员 会一直提示
If($_POST['num1'] == null || $_POST['num2'] == null) {
throw new Typecho_Widget_Exception(_t('验证码异常.', '评论失败'));
并没有对已登录的用户进行判断?
插件设置一下,不要留空
好的,因为是根据您的代码非插件实现的,增强版中还是要加入判断是否博主即可。已经解决了
https://www.76fx.com/137.html
老板,你现在这个验证码里的中文是怎么实现的啊,可以更新一下这篇文章吗?求教,谢谢!
很简单的,有空了吧,最近懒得码字
不错,有用