php中文漢字驗證碼程式

來源:互聯網
上載者:User
 代碼如下 複製代碼


<?php   
/*   
* 檔案:chinesechar.php   
* 作用:漢字資料儲存   
*/   
$ChineseChar = array("人","出","來","友","學","孝","仁","義","禮","廉","忠","國","中","易","白","者","火 ","土","金","木","雷","風","龍","虎","天","地",   "生","暈","菜","鳥","田","三","百","錢","福 ","愛","情","獸","蟲","魚","九","網","新","度","哎","唉","啊","哦","儀","老","少","日",   "月 ","星");   
?>   

<?php   
/*   
* 檔案:check.php   
* 作用:驗證   

*/   
session_start();   
$errorMSG = '';   
//驗證使用者輸入是否和驗證碼一致   
if(!is_null($_POST['check']))   
{   
        if (strcasecmp($_SESSION['code'],$_POST['code'])==0)   
            $errorMSG = "<p style="font-size:12px;color:#009900">驗證成功!</p>";   
        else   
            $errorMSG = "<p style="font-size:12px;color:#FF0000">驗證失敗!</p>";   
}   
?>   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
</head>   
<body>   
<?php   
if($errorMSG){   
echo $errorMSG;   
}   
?>   
<form action=<?php echo $_SERVER['PHP_SELF']?> method=post>   
請輸入驗證碼:<input type="text" name="code" style="width:   
80px"><img src="code.php">   
<br>   
<input type="submit" name="check" value="提交驗證碼">   
</form>   
</body>   
</html>   

<?php   
/*   
* 檔案:code.php   
* 作用:驗證碼產生   
 
* 特注:由 沒牙的草 指導 著作權轉載註明出處!有付出才會有收穫! 
*/   
include_once("chinesechar.php");   
session_start();   
// 設定 content-type   
header("Content-type: image/png");   
// 建立圖片   
$im = imagecreatetruecolor(120, 30);   

// 建立顏色   
$fontcolor = imagecolorallocate($im, 255, 255, 255);   
$bg = imagecolorallocate($im, 0, 0, 0);   

// 設定文字   
for($i=0;$i<4;$i++) $text .= $ChineseChar[(array_rand($ChineseChar))];   

$_SESSION['code'] = $text;   
// 設定字型   
$font = 'simkai.ttf';   

// 添加文字   
imagettftext($im, 18, 0, 11, 21, $fontcolor, $font, iconv("GB2312","UTF-8",$text));   

// 輸出圖片   
imagepng($im);   
imagedestroy($im);   
?>

如果想把上面的程式改成英文數字,只要在chinesechar.php 裡面的數組中文改成數字或字母就KO了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.