php验证码生成程序代码

本文章给大家介绍利用session存储与gd库一并生成验证码程序,同时会加入一些干扰元素,这样就可以简单的防机器注册了,下面我来给各位同学介绍介绍.

PHP验证码并生成图片程序,采用了session识别,稍微改进了一下目前网络上流传的PHP验证码,加入杂点,数字颜色随机显示,控制4位数字显示,话不多说了,程序如下,分享出来.

新建yz.php验证码生成文件,以下代码需要打开php的GD库,修改php.in文件的配置,把已经注释掉的行之前的分号取消即可:extension=php_gd2.dll,代码如下:

  1. <?php
  2. class ValidationCode
  3. {
  4. //属性
  5. private $width;
  6. private $height;
  7. private $codeNum;
  8. private $image;
  9. private $disturbColorNum; //干扰元素数目
  10. private $checkCode;
  11. function __construct($width=80,$height=20,$codeNum=4)
  12. {
  13. $this->width=$width;
  14. $this->height=$height;
  15. $this->codeNum=$codeNum;
  16. $number=floor($width*$height/15);
  17. if($number>240-$codeNum)
  18. {
  19. $this->disturbColorNum=240-$codeNum;
  20. }else
  21. {
  22. $this->disturbColorNum=$number;
  23. }
  24. $this->checkCode=$this->createCheckcode();
  25. }
  26. function getCheckCode()
  27. {
  28. return $this->checkCode;
  29. }
  30. private function createImage(){
  31. $this->image=imagecreatetruecolor($this->width,$this->height);
  32. $backcolor=imagecolorallocate($this->image,rand(225,255),rand(225,255),rand(255,255));
  33. imagefill($this->image,0,0,$backcolor);
  34. $border=imagecolorallocate($this->image,0,0,0);
  35. imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
  36. }
  37. private function setDisturbColor(){
  38. for($i=0;$i<$this->disturbColorNum;$i++){
  39. $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  40. imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  41. }
  42. for($i=0;$i<10;$i++)
  43. {
  44. $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  45. imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,300),55,44,$color);
  46. }
  47. }
  48. private function outputText($font){
  49. for($i=0;$i<$this->codeNum;$i++)
  50. {
  51. $fontcolor=imagecolorallocate($this->image,rand(0,128),rand(0,128),rand(0,128));
  52. if($font")
  53. {
  54. $fontsize=rand(3,5);
  55. $x=floor($this->width/$this->codeNum)*$i+5;
  56. $y=rand(0,$this->height-15);
  57. imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor);
  58. }
  59. else
  60. {
  61. $fontsize=rand(12,16);
  62. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  63. $y=rand($fontsize,$this->height-8);
  64. imagettftext($this->image,$fontsize,rand(-45,45),$x,$y,$fontcolor,$fontFace,$this->checkCode{$i});
  65. }
  66. }
  67. }
  68. private function createCheckCode(){
  69. $code="23456789abcdefghijkmnpqrstuvwrst";
  70. $str="";
  71. for($i=0;$i<$this->codeNum;$i++)
  72. {
  73. $char=$code{rand(0,strlen($code)-1)};
  74. $str.=$char;
  75. }
  76. return $str;
  77. }
  78. private function outputImage()
  79. {
  80. if(imagetypes()&IMG_GIF)
  81. {
  82. header("Content-Type:image/gif");
  83. imagepng($this->image);
  84. }else if(imagetypes()&IMG_JPG)
  85. {
  86. header("Content-Type:image/jpeg");
  87. imagepng($this->image);
  88. }else if(imagetypes()&IMG_PNG)
  89. {
  90. header("Content-Type:image/png");
  91. imagepng($this->image);
  92. }else if(imagetypes()&IMG_WBMP){
  93. header("Content-Type:image/vnd.wap.wbmp");
  94. imagepng($this->image);
  95. }else
  96. {
  97. die("PHP不支持图片验证码");
  98. }
  99. }
  100. //通过该方法向浏览器输出图像
  101. function showImage($font)
  102. {
  103. //创建图像背景
  104. $this->createImage();
  105. //设置干扰元素
  106. $this->setDisturbColor();
  107. //向图像中随机画出文本
  108. $this->outputText($fontFace);
  109. //输出图像
  110. $this->outputImage();
  111. }
  112. function __destruct()
  113. {
  114. imagedestroy($this->image);
  115. }
  116. }
  117. function checklogin(){
  118. if(emptyempty($_POST['name']))
  119. die( '用户名不能为空');
  120. if(emptyempty($_POST['password']))
  121. die("密码不能为空");
  122. if($_SESSION['code']!=$_POST['vertify'])
  123. die("验证码输入不正确".$_SESSION['code']);
  124. $username=$_POST['name'];
  125. $password=md5($_POST['password']);
  126. //检查是否存在
  127. conndb($username,$password);
  128. }
  129. function conndb($name="",$ps=""){
  130. $conn=mysql_connect('localhost','root','123456');
  131. if(!$conn) die("数据库连接失败".mysql_error());
  132. mysql_select_db('5kan',$conn) or die('选择数据库失败'.mysql_error());
  133. mysql_set_charset('utf8',$conn);
  134. $sql="select id from k_user where username='{$name}' and password='{$ps}'";
  135. $result=mysql_query($sql) or die("SQL语句错误".mysql_error());
  136. if(mysql_num_rows($result)>0) die("登录成功");
  137. else die("用户名或者密码错误");
  138. mysql_close($conn);
  139. }
  140. session_start();
  141. if(!isset($_POST['randnum']))
  142. {//开源代码phpfensi.com
  143. $code=new ValidationCode(120,20,4);
  144. $code->showImage("comicbd.ttf"); //显示在页面
  145. $_SESSION['code']=$code->getCheckCode();//保存在服务器中
  146. }
  147. else
  148. {
  149. checklogin();
  150. }
  151. ?>

到具体调用的地方,用这样的形式:<img src="/yz.php" align="absmiddle" />就可以了;验证的时候验证session:$_SESSION['VCODE']的值就可以了,还可以对以上代码稍微改进,改成两个数字相加求和的形式.