PHP实现简单求极限验证码

去除脚本小子以及小学生的好办法!

建议收藏

 


session_start();//开启一个服务端会话
header("Cache-Control: no-cache, must-revalidate");//不缓存
header("Content-Type:image/png");//返回图片类型
//$DEBUG=$_GET['debug'];
$Image= imagecreatetruecolor(180, 80);//画布大小 宽180 高80
$color = imagecolorallocate($Image, 255, 255, 255);//
imagefill($Image, 0, 0, $color);
$font_msyh = "st.ttf";//宋体
$black=ImageColorAllocate($Image, 0, 0, 0);
$red=ImageColorAllocate($Image, 255, 0, 0);
$green=ImageColorAllocate($Image, 0, 255, 0);
$blue=ImageColorAllocate($Image, 0, 0, 255);
$colorset=array($black,$red,$green,$blue);
$dataset=generate();
imagettftext($Image,15,0,10,40,$black,$font_msyh,"lim");
imagettftext($Image,10,0,10,52,$black,$font_msyh,"x"); 
imagettftext($Image,10,0,22,52,$black,$font_msyh,"→");
imagettftext($Image,10,0,35,52,$black,$font_msyh,"$dataset[0]");
ImageLine($Image,50,40,130,40,$black) ;
imagettftext($Image,15,0,50,30,$black,$font_msyh,"$dataset[1]"); 
imagettftext($Image,15,0,60,30,$black,$font_msyh,"x"); 
if($dataset[4]!=1){
    imagettftext($Image,10,0,70,20,$black,$font_msyh,"$dataset[4]"); //当指数不为1时才打印出来
}
imagettftext($Image,15,0,85,30,$black,$font_msyh,"+");
imagettftext($Image,15,0,105,30,$black,$font_msyh,"$dataset[2]");
imagettftext($Image,15,0,60,60,$black,$font_msyh,"x"); 
imagettftext($Image,15,0,85,60,$black,$font_msyh,"+"); 
imagettftext($Image,15,0,105,60,$black,$font_msyh,"$dataset[3]"); 
$answer=($dataset[1]*pow($dataset[0],$dataset[4])+$dataset[2])/($dataset[0]+$dataset[3]);
$answer1=round($answer,3);
imagettftext($Image,10,0,5,75,$red,$font_msyh,"(四舍五入)结果保留三位小数"); 
/*ImageLine($Image,5,10,5,70,$black) ;
ImageLine($Image,85,10,85,70,$black) ;*/
$_SESSION['authcode'] = $answer1;
if($DEBUG=="on"){
    imagettftext($Image,10,0,120,10,$blue,$font_msyh,"Debug:on");//调试答案正式使用请注释掉本行
    imagettftext($Image,10,0,10,10,$black,$font_msyh,$answer);//调试答案正式使用请注释掉本行
    imagettftext($Image,10,0,10,20,$black,$font_msyh,$answer1);//调试答案正式使用请注释掉本行
}
imagepng($Image);//显示出图片
function generate(  )
{
    for ($i = 0; $i < 5; $i++) {
        if($i==0){
            $gen[$i]= rand(0,9);
        }else{
            $gen[$i]= rand(1,9);
        }if($i==4){
            $gen[$i]= rand(0,3);
        }

    }
    return $gen;
}

在主题内function 加入

重要

顶部加入 session_start();
否则登录页面无法获取导session 从而与用户的输入比较
function v_login(){
?>
<p><label for="vcode">验证码<input type="text" name="vcode" value="" size="20" class="input" tabindex="20" /></label></p>
<p>请计算结果<br/> <img id="captcha_img" src="/authcode.php" onclick="document.getElementById('captcha_img').src='/authcode.php'"/></p></p>
<?
}
add_filter('wp_authenticate_user', 'vcode_v', 10, 2);
function vcode_v($user, $username='', $password=''){
if (isset($_POST['wp-submit'])){
if(!($_POST['vcode'] == $_SESSION['authcode'])){
remove_filter('authenticate', 'vcode_v', 20, 3);
$error = new WP_Error();
$error->add('incorrect_password', '<strong>错误</strong>:验证码不正确。');
return $error;
}
}
return $user;
}
add_filter('wp_authenticate_user', 'vcode_v', 10, 2);
add_action('login_form', 'v_login');
点赞