教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php GD库生成验证码的实例

php GD库生成验证码的实例

发布时间:2015-08-20   编辑:jiaochengji.com
还是那个我们喜欢的php GD库,脚本学堂 刚刚为大家介绍完 php GD库上传图片并创建缩略图的代码 ,这里再为大家推出一篇 php GD库生成验证码的实例。

还是那个我们喜欢的php GD库,教程集 刚刚为大家介绍完 php GD库上传图片并创建缩略图的代码 ,这里再为大家推出一篇 php GD库生成验证码的实例。
有兴趣的朋友,不妨继续阅读吧。

配置与测试,参照上篇:php GD库上传图片并创建缩略图的代码 。

1、表单auth.html
 

复制代码 代码示例:
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>验证码</title>
</head>
<body>
<h1>请输入验证码</h1>
<form action="check_auth.php" method="post">
   <input name="auth" type="text">
   <img src="auth.php" border="0" />
   <input type="submit" value="提交">
</form>
</body>
</html>

2、生成验证码 auth.php
 

复制代码 代码示例:

<?php
   session_start();
   header("Content-type:image/png");

   $img_width=100;
   $img_height=20;

   srand(microtime()*100000);
   for($i=0;$i<4;$i++)
   {
        $new_number.=dechex(rand(0,15));
   }

   $_SESSION[check_auth]=$new_number;
   $new_number=imageCreate($img_width,$img_height);//创建图象
   ImageColorAllocate($new_number,255,255,255);  //设置背景色为白色

   for($i=0;$i<strlen($_SESSION[check_auth]);$i++)
   {
       $font=mt_rand(3,5);
       $x=mt_rand(1,8) + $img_width*$i/4;
       $y=mt_rand(1,$img_height/4);
       $color=imageColorAllocate($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//设置字符颜色
       imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//输出字符
   }

   ImagePng($new_number);
   ImageDestroy($new_number);
?>

3、提交页面 check_auth.php
 

复制代码 代码示例:

<?php
   session_start();
   $auth=$_POST['auth'];

   if(empty($auth))
   {
       echo '错误:验证码不能为空';
       die;
   }

   if($auth==$_SESSION['check_auth'])
   {
       echo '正确';
   }
   else
   {
       echo '错误:验证码输入错误';
   }
?>

您可能感兴趣的文章:
windows下开启PHP GD库的方法
php绘图不显示图片怎么办
php gd库的基础知识
Linux下开启PHP GD库支持
php验证码(GD库生成验证码)的例子
php彩色验证码的简单例子
php验证码大全(实例分享)
php开启GD库(windows环境)
用php生成带有雪花背景的验证码
php GD库函数getimagesize()的用法

关键词: gd库  php gd  php验证码  验证码   
[关闭]
~ ~