简单实用的PHP验证码类

一个简单实用的php验证码类,分享出来 ,供大家参考。

代码如下:

  1. <?php  
  2.   
  3. /** 
  4.  @ php 验证码类 
  5.  
  6. @ http://www.jbxue.com 
  7.  
  8. */  
  9. Class code  
  10. {  
  11.  var $width =80; //图片的宽  
  12.  var $hight =50; //图片的高  
  13.  var $image;  
  14.  var $red =69; //图片的RGB颜色  
  15.  var $green =188; //红  
  16.  var $blue =105 ;//绿  
  17.  var $pix =100 ;//蓝  
  18.  var $pixcolor; //杂色颜色;  
  19.  var $pixred = 255 ; //红  
  20.  var $pixgreen = 255;//绿  
  21.  var $pixblue = 255; //蓝  
  22.  var $txt=null;//验证码文字  
  23.  var $txtcode=null;  
  24.  var $txtsub=null;  
  25.  var $pixnum = 300; //杂点数量  
  26.  var $i=0;  
  27.  var $widthpx=0;  
  28.  var $highty=0;  
  29.  var $txtreg=20;  
  30.  var $txtgreen=30;  
  31.  function createimage()  //创建一张图并填色  
  32.  {  
  33.   $this->image = imagecreate($this->width,$this->hight);  
  34.   $this->color = imagecolorallocate($this->image,$this->red,$this->green,$this->blue);  
  35.   return imagefill($this->image,0,0,$this->color);  
  36.  }  
  37.  function createpix() //干扰因素  
  38.  {  
  39.   for($this->i=1;$this->i<$this->pixnum;$this->i++)  
  40.   {  
  41.     $this->widthpx = rand(0,$this->width);  
  42.     $this->highty = rand(0,$this->hight);  
  43.    $this->pixcolor = imagecolorallocate($this->image,$this->pixred,$this->pixgreen,$this->pixblue);  
  44.      imagesetpixel($this->image,$this->widthpx,$this->highty,$this->pixcolor);  
  45.      
  46.   }  
  47.    
  48.  }  
  49.    
  50.   function gettxt() //创建验证码文字  
  51.  {  
  52.   $this->txt = array("A","B","C","D","E","F","G","H","I","M","Y","a","b","e","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");  
  53.   for($this->i=0;$this->i<6;$this->i++)  
  54.   {  
  55.    $this->sub = $this->txt[rand(0,29)];  
  56.    $this->txtcode.= $this->sub;  
  57.   }  
  58.   session_start();  
  59.   $_SESSION["code"] = $this->txtcode;  
  60.  }  
  61.    
  62.  function createstring() //创建验证码图片  
  63.  {  
  64.   imagettftext($this->image,20,5,0,40,$this->pixcolor,"C:WINDOWSFontssimsun.ttc ",$this->txtcode);  
  65.   header("content-type:image/png");  
  66.   return imagepng($this->image);  
  67.      imagedestroy($this->image);  
  68.  }  
  69.  function getcodeimage()//获得验证码图片  
  70.  {  
  71.   $this->createimage();  
  72.   $this->createpix();  
  73.   $this->gettxt();  
  74.   $this->createstring();  
  75.  }  
  76. }  
  77. ?>  
  78. <?php       
  79. $text = new code;  
  80. $text->createimage();  
  81. $text->gettxt();  
  82. $text->createpix();  
  83. $text->createstring();  
  84. ?>  
以上就是本节 php教程 提供的例子,php验证码在实际的编程中用的比较多,用户注册、会员评论等功能中都会用到,好好学习下吧。
原文地址:https://www.cnblogs.com/linuxnotes/p/3486831.html