跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

php GD库生成验证码的实例

验证码请输入验证码复制代码2、生成验证码 auth.phpsession_start();header("content-type:image/png");$img_width=100;$img_height=20;srand(microtime()*100000);for($i=0;$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{$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);//输出字符}立即学习“PHP免费学习笔记(深入)”;ImagePng($new_number);ImageDestroy($new_number);?>复制代码3、提交页面 check_auth.phpsession_start();$auth=$_POST['auth'];if(empty($auth)){echo '错误:验证码不能为空';die;}if($auth==$_SESSION['check_auth']){echo '正确';}else{echo '错误:验证码输入错误';}?>复制代码

相关文章