验证码

来源:互联网 发布:wind数据库怎么用 编辑:程序博客网 时间:2024/05/29 02:59
  

check_code.php编写图片

 

<?php
 class CheckCode {
  private $image;
  private $width;
  private $height;
  private $codeNum;
  private $disturbNum;
  private $checkCode;
  
  function __construct($width=100,$height=30,$codeNum=4){
   $this->width=$width;
   $this->height=$height;
   $this->codeNum=$codeNum; 
   $this->checkCode=$this->createCheckCode();
   $number = floor($width*$height/15);
   if($number > 240-$codeNum){
    $this->disturbNum=240-$codeNum;
   }else{
    $this->disturbNum=$number;
   }
   
   
  }

  
  //将验证码输出到浏览器上
  function showImage(){
   //第一步创建验证码图像
   $this->createimage();
   //第二步加入干扰素
   $this->setdisturbcode();
   //第三步向图片中随机的写入文本内容
   $this->outputtext();
   //输出图像
   $this->outputimage();
  }
  
  //客户端保存验证码
  function  getcodeimage(){
   return $this->checkCode;
   echo $this->checkCode;
  }
   
    

  //制作验证码
  private function createimage(){
   $this->image=imagecreatetruecolor($this->width,$this->height);
   //随机的变换图片的背景颜色
   $bgcolor=imagecolorallocate($this->image,rand(225,255),rand(225,255),rand(225,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 setdisturbcode(){
   for($i=0; $i<$this->disturbNum; $i++){
   $pixelcolor=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
   imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$pixelcolor);
   }
   
   for($i=0; $i<10; $i++){
    $circlecolor=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
    imagearc($this->image,rand(3,$this->width),rand(3,$this->height),rand(10,$this->width-5),rand(10,$this->height-5),45,180,$circlecolor);
   }
  }
  
  //创建验证字符
  private function createCheckCode(){
   $str="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIGKMNPQRSTUVWXYZ";
   $code="";
   for($i=0; $i<$this->codeNum; $i++){
    $code.=$str{rand(0,strlen($str)-1)};
   }
   return $code;
   
  }
  
  //插入字符
  private function outputtext(){
   for($i=0; $i<$this->codeNum; $i++){
    $fontcolor=imagecolorallocate($this->image,rand(0,120),rand(0,120),rand(0,120));
    $fontsize=rand(3,6);
    $y=rand(0,$this->height-15);
    $x=floor($this->width/$this->codeNum)*$i+5;
    //imagettftext($this->image,$fontsize,0,$x,$y,$fontcolor,"../font/simhei.ttf",$this->checkCode{$i});
    imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor);
   }
  }
  
  //输出验证码
  private function outputimage(){
   if(imagetypes() & IMG_PNG){
   header("Content-type:image/png");
   imagepng($this->image);
   } else  if(imagetypes($img) & IMG_JPEG){
   header("Content-type:image/jpeg");
   imagejpeg($this->image);
   } else  if(imagetypes($img) & IMG_GIF){
   header("Content-type:image/gif");
   imagegif($this->image);
   } else{
   echo "<script> alert('php不支持此类型的图片'); </script>";
      }
 
  }
 }
 
 
?>

 

 

调用图片 并运用session

<?php
 session_start();
 
 include("check_code.php");
 
 $code=new CheckCode(100,30,4);
 
 $code->showImage();
 
 $_SESSION["code"]=$code->getcodeimage();
 
 
?>

 

login.php显示验证码

<?php
 session_start();
 $a=@strtoupper($_GET["input1"]);
 $b=@strtoupper($_SESSION["code"]);
 if($a==$b){
  echo "进入";
 }else{
  echo "错误";
 }
 
 ?>
 <form action="" method="get" name="form1">
  <input type="text" name="input1" onkeyup="if(this.value!=this.value.toUpperCase())  this.value=this.value.toUpperCase()">   <img src="code.php" onclick="this.src='code.php?'+Math.random()"> <font size="4">点击图片更换</font> <hr>
  <input type="submit" value="提交">
 </form>   

原创粉丝点击