php验证码的三个实例代码分享
发布时间:2016-09-06 编辑:jiaochengji.com
本文介绍下,php实现的三个图片验证码,有需要的朋友,可以参考学习下。
验证码-代码1:
<?php //checkNum.php session_start(); function random($len) { $srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; mt_srand();//配置乱数种子 $strs=""; for($i=0;$i <$len;$i++){ $strs.=$srcstr[mt_rand(0,35)]; } return strtoupper($strs); } $str=random(4); //随机生成的字符串" title="字符串" >字符串 $width = 50; //验证码图片的宽度 $height = 25; //验证码图片的高度 @header("Content-Type:image/png"); $_SESSION["code"] = $str; //echo $str; $im=imagecreate($width,$height); //背景色 $back=imagecolorallocate($im,0xFF,0xFF,0xFF); //模糊点颜色 $pix=imagecolorallocate($im,187,230,247); //字体色 $font=imagecolorallocate($im,41,163,238); //绘模糊作用的点 mt_srand(); for($i=0;$i <1000;$i++) { imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix); } imagestring($im, 5, 7, 5,$str, $font); imagerectangle($im,0,0,$width-1,$height-1,$font); imagepng($im); imagedestroy($im); $_SESSION["code"] = $str; session_destroy(); ?>
验证码--代码2:
<?php session_start(); $authnum=random(6);//验证码字符. $_SESSION['yan']=$authnum; //生成验证码图片 Header("Content-type: image/PNG"); $im = imagecreate(55,18); //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。 $red = ImageColorAllocate($im, 153,51,0); //设置背景颜色 $white = ImageColorAllocate($im, 255,204,0);//设置文字颜色 $gray = ImageColorAllocate($im, 102,102,0); //设置杂点颜色 imagefill($im,55,18,$red); for ($i = 0; $i < strlen($authnum); $i++) { // $i%2 == 0?$top = -1:$top = 3; imagestring($im, 6, 13*$i+4, 1, substr($authnum,$i,1), $white); //int imagestring ( resource image, int font, int x, int y, string s, int col) //imagestring() 用 col 颜色将字符串" title="字符串" >字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。 } for($i=0;$i<100;$i++) //加入干扰象素 { imagesetpixel($im, rand()%55 , rand()%18 , $gray); //int imagesetpixel ( resource image, int x, int y, int color) //imagesetpixel() 在 image 图像中用 color 颜色在 x, y 坐标(图像左上角为 0, 0)上画一个点。 } ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件 ImageDestroy($im);//销毁一图像 //产生随机数函数 function random($length) { $hash = ''; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } ?>
验证码--代码3:
<?php session_start(); //生成验证码图片 Header("Content-type: image/PNG"); srand((double)microtime()*1000000); $im = imagecreate(62,20); $black = ImageColorAllocate($im, 0,0,0); $white = ImageColorAllocate($im, 255,255,255); $gray = ImageColorAllocate($im, 200,200,200); imagefill($im,68,30,$gray); while(($authnum=rand()%100000)<10000); //将四位整数验证码绘入图片 imagestring($im, 5, 10, 3, $authnum, $white); for($i=0;$i<200;$i++) //加入干扰象素 { $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); } ImagePNG($im); ImageDestroy($im); $_SESSION['SESSION_VALIDATE_CODE'] = $authnum.''; ?>您可能感兴趣的文章:
php验证码简单函数代码(附效果图)
分享一个php 验证码类及调用示例
一个php验证码的封装类
php自定义大小验证码的实例代码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php验证码函数使用的例子
php5验证码类(简易实用型)
php验证码(GD库生成验证码)的例子
php点击验证码实时刷新的实现代码
php图片验证码的例子
php彩色验证码的简单例子
php验证码刷新与局部刷新的实现方法
php GD库生成验证码的实例
php生成验证码的例子
php随机验证码 php生成随机验证码(图文)
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码
您可能感兴趣的文章:
php彩色验证码的简单例子
php验证码大全(实例分享)
php点击验证码实时刷新的实现代码
php5验证码类(简易实用型)
php验证码的三个实例代码分享
php图片验证码的例子
php验证码简单函数代码(附效果图)
php生成扭曲及旋转的验证码图片的实例代码
用php生成带有雪花背景的验证码
分享一个php 验证码类及调用示例
上一篇:php 文件缓存数据类的代码分享
下一篇:php操作SQLite类的代码分享
[关闭]