PHP生成中文验证码并检测对错实例-php教程

资源魔 39 0
未标题-6.png

PHP天生中文验证码并检测对错实例,中文验证码的例子仍是比拟少的,明天给各人分享一下,支持自界说中文、字体、布景色等

58d415db5523ad237523ee1290f094003a3.jpg

天生验证码,留意font字体门路要对,不然显示图片没有存正在

session_start(); 
 
//1>设置验证码图片巨细的函数 $image = imagecreatetruecolor(200, 60); 
//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image, 255, 255, 255); //#ffffff //6>区域填充 int imagefill(int im, int x, int y, int col)  (x,y) 所正在的区域着色,col 示意欲涂上的颜色 imagefill($image, 0, 0, $bgcolor); 
//7>设置ttf字体 $fontface = 'simhei.ttf'; 
//7>设置字库,完成简略的数字储蓄 $str = '天生中文验证码并检测对错实例';
//str_split()切割字符串为一个数组,一个中文正在utf_8为3个字符 $strdb = str_split($str, 3); 
//>11 $captcha_code = ''; 
//8>天生随机的男人 for ($i = 0; $i < 4; $i++) { 
    //设置字体颜色,随机颜色 
    $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));            //0-120深颜色 
    //随机拔取中文 
    $in = rand(0, count($strdb)); 
    $cn = $strdb[$in]; 
    //将中文记载到将保留到session的字符串中 
    $captcha_code .= $cn; 
    /* imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color, 
      string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐标,颜色,字体门路,文本字符串 
      mt_rand()天生更好的随机数,比rand()快四倍 */ 
    imagettftext($image, mt_rand(20, 24), mt_rand(-60, 60), (40 * $i + 20), mt_rand(30, 35), $fontcolor, $fontface, $cn); 
} 
//11>存到session $_SESSION['sucaihuo_code'] = $captcha_code;

Ajax检检验证码

function checkCode() { 
    $.post("ajax.php", {code: $("#input_code").val()}, function(data) { 
        if (data == '1') { 
            alert("验证码正确!"); 
        } else { 
            alert("验证码谬误!"); 
        } 
 
    }, "json") 
}

保举教程:PHP验证码完好视频教程

以上就是PHP天生中文验证码并检测对错实例的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 中文验证码

抱歉,评论功能暂时关闭!