电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > PHP教程 > 正文
PHP生成验证码之动态随机生成验证码类代码
PHP生成验证码之动态随机生成验证码类代码
2010-4-8 9:10:07  文/佚名   出处:cnblogs   

本文介绍一下PHP动态随机生成验证码的类,来看下吧。

  1 <?php 2 /************************************************************************ 3 //FILE:ImageCode 4 //DONE:生成动态验证码类 5 //DATE"2010-3-31 6 //Author:www.5dkx.com 5D开心博客 7 ************************************************************************/ 8 class ImageCode{ 9 private $width; //验证码图片宽度 10 private $height; //验证码图片高度 11 private $codeNum; //验证码字符个数  12 private $checkCode; //验证码字符 13 private $image; //验证码画布 14 /************************************************************************ 15 // Function:构造函数 16 // Done:成员属性初始化 17 // Author:www.5dkx.com 5D开心博客 18 ************************************************************************/ 19 function __construct($width=60,$height=20,$codeNum=4) 20 { 21 $this->width = $width; 22 $this->height = $height; 23 $this->codeNum = $codeNum; 24 $this->checkCode = $this->createCheckCode(); 25 } 26 function showImage() 27 { 28 $this->getcreateImage(); 29 $this->outputText(); 30 $this->setDisturbColor(); 31 $this->outputImage(); 32 } 33 function getCheckCode() 34 { 35 return $this->chekCode; 36 } 37 private function getCreateImage() 38 { 39 $this->image = imagecreatetruecolor($this->width,$this->height); 40 $back = imagecolorallocate($this->image,255,255,255); 41 $border = imagecolorallocate($this->image,255,255,255); 42 imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 43 //使用纯白色填充矩形框,这里用的话后面干扰码失效 44 /*如果想用干扰码的话使用下面的*/ 45 //imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 46 } 47 private function createCheckCode() 48 { 49 for($i=0;$i<$this->codeNum;$i ) 50 { 51 $number = rand(0,2); 52 switch($number) 53 { 54 case 0: $rand_number = rand(48,57); break;//数字 55 case 1: $rand_number = rand(65,90);break;//大写字母 56 case 2: $rand_number = rand(97,122);break;//小写字母 57 } 58 $asc = sprintf("%c",$rand_number); 59 $asc_number = $asc_number.$asc; 60 } 61 return $asc_number; 62 } 63 private function setDisturbColor()//干扰吗设置 64 { 65 for($i=0;$i<=100;$i ) 66 { 67 //$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255)); 68 $color = imagecolorallocate($this->image,255,255,255); 69 imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 70 } 71 //$color = imagecolorallocate($this->image,0,0,0); 72 //imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 73 74 75 } 76 private function outputText() 77 { 78 //随机颜色、随机摆放、随机字符串向图像输出 79 for($i=0;$i<=$this->codeNum;$i ) 80 { 81 $bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255)); 82 $x = floor($this->width/$this->codeNum)*$i 3; 83 $y = rand(0,$this->height-15); 84 imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color); 85 } 86 } 87 88 private function outputImage() 89 { 90 if(imagetypes()&IMG_GIF) 91 { 92 header("Content_type:image/gif"); 93 imagegif($this->image); 94 } 95 elseif(imagetypes()&IMG_JPG) 96 { 97 98 header("Content-type:image/jpeg"); 99 imagejpeg($this->image,"",0.5); 100 } 101 elseif(imagetypes()&IMG_PNG) 102 { 103 header("Content-type:image/png"); 104 imagejpeg($this->image); 105 } 106 elseif(imagetypes()&IMG_WBMP) 107 { 108 header("Content-type:image/vnd.wap.wbmp"); 109 imagejpeg($this->image); 110 } 111 else 112 { 113 die("PHP不支持图像创建"); 114 } 115 } 116 117 function __destruct() 118 { 119 imagedestroy($this->image); 120 } 121 } 122 123 /*显示*/ 124 /******************************************************************* 125 session_start(); 126 $image = new ImageCode(60,20,4); 127 $image->showImage(); 128 $_SESSION['ImageCode'] = $image->getCheckCode(); 129 *******************************************************************/ 130 131 ?>

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    解决PHP设置使用date或mktime函数问…
    分享PHP实现Web系统单点登录的方法
    PHP实用对付IP攻击升级程序改进
    超级有用网站防IP攻击代码详解
    Cannot start session without erro…
    PHP开发应用程序:生成随机字符串的方…
    PHP开发技巧:PHP网站发开实用技巧集…
    详解如何正确配置PHP开发环境
    PHP里实现汉字转区位码的示例代码
    PHP实现列出目录内容与删除目录的内…
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号