
PHP生成中文驗(yàn)證碼并檢測對(duì)錯(cuò)實(shí)例,中文驗(yàn)證碼的例子還是比較少的,今天給大家分享一下,支持自定義中文、字體、背景色等

生成驗(yàn)證碼,注意font字體路徑要對(duì),否則顯示圖片不存在
session_start(); //1>設(shè)置驗(yàn)證碼圖片大小的函數(shù) $image = imagecreatetruecolor(200, 60); //5>設(shè)置驗(yàn)證碼顏色 imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image, 255, 255, 255); //#ffffff //6>區(qū)域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區(qū)域著色,col 表示欲涂上的顏色 imagefill($image, 0, 0, $bgcolor); //7>設(shè)置ttf字體 $fontface = 'simhei.ttf'; //7>設(shè)置字庫,實(shí)現(xiàn)簡單的數(shù)字儲(chǔ)備 $str = '生成中文驗(yàn)證碼并檢測對(duì)錯(cuò)實(shí)例';
//str_split()切割字符串為一個(gè)數(shù)組,一個(gè)中文在utf_8為3個(gè)字符 $strdb = str_split($str, 3); //>11 $captcha_code = ''; //8>生成隨機(jī)的漢子 for ($i = 0; $i < 4; $i++) { //設(shè)置字體顏色,隨機(jī)顏色 $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); //0-120深顏色 //隨機(jī)選取中文 $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 ) 幕布 ,尺寸,角度,坐標(biāo),顏色,字體路徑,文本字符串 mt_rand()生成更好的隨機(jī)數(shù),比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;
html' target='_blank'>Ajax檢測驗(yàn)證碼
function checkCode() { $.post("ajax.php", {code: $("#input_code").val()}, function(data) { if (data == '1') { alert("驗(yàn)證碼正確!"); } else { alert("驗(yàn)證碼錯(cuò)誤!"); } }, "json") }推薦教程:PHP驗(yàn)證碼完整視頻教程
以上就是PHP生成中文驗(yàn)證碼并檢測對(duì)錯(cuò)實(shí)例的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注 其它相關(guān)文章!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選