页面

2011年5月28日星期六

我的PHP验证码

       前天看了PHP图像处理的视频,于是学着视频自己也做了一个验证码的小例子。以下对验证码类进行说明。
      文件:
       CheckCode.class.php  // 验证码类文件
       demon.php                //前端web页面调用验证码类的中间文件
       demo.html                //前端页面(以一个简单的登录程序测试)
       login.php                 //登录验证
验证码类,可以设定图片的大小和验证码字符的个数!



CheckCode.class.php 内容如下:

<?php
/**
 *
 * Enter description here ...
 * @author Kyle Wong
 * @version 1.0
 *
 *
 */
class CheckCode{
   private $width;   //验证码图片宽度
   private $height;  //验证码图片高度
   private $charNum;  //验证码字符个数
   private $code;     //验证码
   private $image;    //图片资源
  
   function __construct($width=80, $height=20, $codeNum=4){
             $this->width=$width;
             $this->height=$height;
             $this->charNum=$codeNum;
             $this->code = $this->createCode();
    }
   

  //把图像显示在web页面
  function  showImg(){
      //创建图像资源
     $this->createImg();
     //设置干扰点
     $this->setDisturbColor();
     //显示验证码
     $this->painCode();
     
     ob_clean();//清楚缓冲区对象
     //输出图像
     $this->outputImg();
  }
 
 //获取验证码
  function  getCode(){
    return $this->code;
  }
 
//创建验证码
  private function createCode(){
       $string = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
       $code='';
       for($i=0; $i < $this->charNum; $i++){
           $char = $string{rand(0,strlen($string)-1)};
           $code.=$char;
       }
      
       return $code;
  }
 
  //创建图像资源
  private  function createImg(){
   
     $this->image = imagecreatetruecolor($this->width,$this->height);
     
     $bgcolor = imagecolorallocate($this->image,rand(233,255),rand(233,255),rand(233,255));
     
     imagefill($this->image,0,0,$bgcolor);
   
     $borderColor = imagecolorallocate($this->image,0,0,0);
     
     imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$borderColor);
     
   }
  
   //创建干扰点,干扰弧线
   private function setDisturbColor(){
       $disPointNum = floor(($this->width * $this->height)/20);
       //创建干扰点
       for($i=0; $i<$disPointNum; $i++){
           $color = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
           imagesetpixel($this->image, rand(1,$this->width-1), rand(1,$this->height-1), $color);
       }
      
       //创建干扰弧线
       for ($i=0; $i < 20; $i++){
           $color = imagecolorallocate($this->image, rand(0, 180), rand(0, 180), rand(0, 180));
           imagearc($this->image, rand(-8, $this->width), rand(-8, $this->height), rand(30,300), rand(20,200), -45, 45, $color);
       }
  
   }
  
  
   //画出验证码
   private function painCode(){
          
        for($i = 0; $i<$this->charNum; $i++ ){
            $charColor = imagecolorallocate($this->image, rand(0,100), rand(0,100), rand(0,100));
            $charSize = rand(4,5);
            $x = floor(($this->width/$this->charNum)*$i+5);
            $y = rand(1,$this->height-15);
            imagechar($this->image, $charSize, $x, $y, $this->code[$i], $charColor);
        }
   }
  
  
   //图像输出
   private  function outputImg(){
       if(imagetypes() & IMG_PNG){
           header("Content-Type:image/png");
           imagepng($this->image);
       }elseif (imagetypes() & IMG_GIF) {
           header("Content-Type:image/gif");
           imagegif($this->image);
       }elseif (imagetypes() & IMG_JPEG) {
           header("Content-Type:image/jpeg");
           imagejpeg($this->image);
       }elseif (imagetypes() & IMG_WBMP) {
           header("Content-Type:image/vnd.wap.wbmp");
           imagewbmp($this->image);
       }else {
           die("PHP 不支持GD图形库");
       }

   }
   
   
 //销毁资源
    function __destruct(){
        imagedestroy($this->image);
    }
   
   
}

CheckCode类,通过 4步来创建一个验证码图片:
     1.创建图像资源  调用类方法createImg();
     2.设置干扰点  调用类方setDisturbColor();
     3.在图片资源上显示验证码 调用类方法painCode();
     4.把图像输出  调用类方法outputImg();
     而ob_clean()用作清楚缓冲区对象    在我机器上的php版本(vesion 5.3.3)要使用这个方法才能显示图片,具体原因不明!

     demon.php源码:
<?php
session_start();
include 'CheckCode.class.php';
$checkCode = new CheckCode();
$checkCode->showImg();
$_SESSION["code"] = $checkCode->getCode();

   这段代码的主要作用是显示验证码图片,并把验证码保存至session会话中。

demo.html部分代码:
  <form action="login.php" method="post">
user:<input type="text" name = "user"></br>
password:<input type="password" name = "password"></br>
code:<input type="text" name = "code">input the chars in the picture</br>
<img alt="code" src="demon.php?" onclick="this.src='demon.php?'+Math.random()">
</br>
click the image to change another image!</br>
<input type ="submit" value="Login">
<input type="reset"" value="reset">
</form>

这里是简单的一个登录表单,利用验证码登录!点击验证码会刷新图片。

login.php源码:

<?php
session_start();
$user = $_POST["user"];
$password = $_POST["password"];
$code = strtoupper($_POST["code"]);

if ($user=="Kyle" && $password=="ming" && $code==strtoupper($_SESSION["code"])){
    echo "<h1>Login successful!</h1>";
}else {
   echo "<h1>Login faild!</h1>";
 }

       login验证作用,利用demo.html中form表单提交过来的code和session中的code 进行比较达到验证码验证!

OK这就是我写的验证码例子!

没有评论:

发表评论