目录
WordPress在互联网上面有34%的使用率,从业余爱好的博客到新闻网站很多都是使用的WordPress源码,因为使用的人比较多,所以很多的人开发了一些发送垃圾评论的软件,自动化批量的发送垃圾评论,所以在安装时WordPress自带有一个Akismet Anti-Spam的插件,但是还是会有一些漏网之鱼,一般可以开启评论审核不让垃圾评论第一时间显示,但这并不能阻断垃圾评论的产生。所以我们需要验证码防止机器人评论广告信息。
这里爱游建议不安装插件,插件多会影响博客的打开速度,爱游这里分享的方法不用安装任何插件就能现实验证码功能,只需要将代码添加到当前主题的 functions.php 中
代码如下:
function loper_protection_math(){
# 数字加法两个随机数, 范围0~99
$num1=rand(0,9);
$num2=rand(0,9);
echo "<input 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\">";
echo "<label for=\"math\">请输入(计算结果)</label>\n";
}
function loper_protection_pre($commentdata){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:
break;case null:err('错误: 请输入验证码。');
break;default:err('错误: 验证码错误。');}
return $commentdata;}
if($comment_data['comment_type']==''){
add_filter('preprocess_comment','loper_protection_pre');}
function loper_protection_math(){
$num1=substr(md5(mt_rand(0,99)),0,5);
# 英文数字随机数, 范围0~99
echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\">\n";
echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
echo "<label for=\"math\" >请输入( $num1 )</label>\n";
}
function loper_protection_pre($commentdata){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']:
break;case null:err('错误: 请输入验证码。');
break;default:err('错误: 验证码错误。');}
return $commentdata;}
if($comment_data['comment_type']==''){
add_filter('preprocess_comment','loper_protection_pre');}
<?php loper_protection_math();?>
cambrian.render('tail')