发新话题
打印

PHP汉字验证码的实现

PHP汉字验证码的实现

[Copy to clipboard]
PHP CODE:
<html>    
<
head>    
<
meta http-equiv="Content-Type" c>    
</
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">    
<input type="submit" name="check" value="提交验证码">    
</form>    
</body>    
</html>
[Copy to clipboard]
PHP CODE:

<?php    
/*    
* 文件:chinesechar.php     
*/    
$ChineseChar = array("人","出","来","友","学","孝","仁","义","礼","廉","忠","国","中","易","白","者","火 ","土","金","木","雷","风","龙","虎","天","地",  
"生","晕","菜","鸟","田","三","百","钱","福 ","爱","情","兽","虫","鱼","九","网","新","度","哎","唉","啊","哦","仪","老","少","日",  
"月 ","星");    
?>    

[Copy to clipboard]
PHP CODE:

<?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>";    
}    
?>    

[Copy to clipboard]
PHP CODE:

<?php    
/*    
* 文件:code.php    
* 作用:验证码生成    
*/    
include_once("chinesechar.php");    
session_start();    
// 设置 content-type    
header("Content-type: image/png");    
// 创建图片    
$im imagecreatetruecolor(12030);    

// 创建颜色    
$fontcolor imagecolorallocate($im255255255);    
$bg imagecolorallocate($im000);    

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

$_SESSION['code'] = $text;

// 设置字体  [url]http://www.font.com.cn/downlist/s_12_3.html[/url] 有_GBK系列的字体下载,一般GD库都支持的!
$font1 'font/shuizhu.ttf';

// 添加文字    
imagettftext($im1801121$fontcolor$fonticonv("GB2312","UTF-8",$text));    

// 输出图片    
imagepng($im);    
imagedestroy($im);    
?>

[ 本帖最后由 Double 于 2007-12-22 11:28 编辑 ]

TOP

验证码图片显示不了啊??
该怎么解决??

TOP

你看看是不是你调用页面的时候调用错了
或者是你看看你的PHP有没有GD库

TOP

好的。

不错啊。这个看着很简单。

TOP

收藏了,

TOP

发新话题