PHP图片验证码的实现-包括前后台页面

以前输出验证码的时候用过一个方法,在前台用JS生成验证码字符串,再传递到后台用PHP输出验证码图像。这样在验证时就不需要使用$_SESSION传递验证码的值,直接用JS比较生成的字符串和输入的字符串是否相等即可。

        但是这种方法的缺点是结构化编程比较明显,并且感觉脱节比较严重。在网上找了一些生成验证码的方法,也都不太完整,有些只包括生成图像并没有包括完整的验证部分,因此在此给出完整的验证码生成类和验证过程。


index.php

其中<img src="ValidationCode.class.php" id="checkImg">用于输出验证码图像。onClick= "this.src=this.src+'?' + Math.random();" } 用于在单击时重新加载图像。

  1. <?php   
  2.     session_start();                                   //用于在$_SESSION中获取验证码的值  
  3. ?>  
  4. <script type="text/javascript">  
  5.     window.οnlοad=function() {  
  6.         var xmlHttp = false;  
  7.         if (window.XMLHttpRequest) {                    //Mozilla、Safari等浏览器  
  8.             xmlHttp = new XMLHttpRequest();  
  9.         }   
  10.         else if (window.ActiveXObject) {                //IE浏览器  
  11.             try {  
  12.                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
  13.             } catch (e) {  
  14.                 try {  
  15.                     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  16.                } catch (e) {}  
  17.             }  
  18.         }  
  19.         var checkImg=document.getElementById("checkImg");  
  20.         var checkCode=document.getElementById("checkCode");  
  21.         checkImg.οnclick=function() {  
  22.             checkImg.src="ValidationCode.class.php?num="+Math.random();  //以下用于单击验证码时重新加载验证码图片  
  23.             //需要加num=Math.random()以防止图片在本地缓存,单击图片时不刷新显示  
  24.         }  
  25.         checkCode.οnblur=function(){  
  26.             //alert("Hello");  
  27.             xmlHttp.open("POST","checkCode.php",true);  
  28.             xmlHttp.onreadystatechange=function () {  
  29.                 if(xmlHttp.readyState==4 && xmlHttp.status==200) {  
  30.                     var msg=xmlHttp.responseText;    //设置标志用于表示验证码是否正确  
  31.                     if(msg=='1')  
  32.                         document.getElementById("checkResult").innerHTML="正确";  //在这可以设置其中间显示一张图片  
  33.                     else  
  34.                         document.getElementById("checkResult").innerHTML="错误";  
  35.                 }  
  36.             }  
  37.             xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");  
  38.             xmlHttp.send("validateCode="+checkCode.value);  
  39.         }     
  40.     }  
  41. </script>  
  42. <input type="text" id="checkCode">  
  43. <img src="ValidationCode.class.php" id="checkImg"><span id="checkResult">请在此输入验证码</span>  

ValidationCode.class.php

验证码的生成类,要使用$_SESSION['checkCode']将验证码的值存入$_SESSION中。

  1. <?php  
  2. session_start();               //为了将验证码的值保留在$_SESSION中  
  3. class ValidationCode {  
  4.     private $width;  
  5.     private $height;  
  6.     private $codeNum;           //验证码的个数  
  7.     private $image;             //图像资源  
  8.     private $checkCode;         //验证码字符串  
  9.   
  10.     function __construct($width=60,$height=20,$codeNum=4) {  
  11.         $this->width=$width;  
  12.         $this->height=$height;  
  13.         $this->codeNum=$codeNum;  
  14.         $this->checkCode=$this->createCheckCode();  
  15.     }  
  16.     //通过调用该方法向浏览器输出验证码图像  
  17.     function showImage() {  
  18.         $this->createImage();      //第一步:创建背景图像  
  19.         $this->setDisturbColor();  //第二步:设置干扰元素,此处只加了干扰直线  
  20.         $this->outputText();       //第三步:输出验证码  
  21.         $this->outputImage();      //第四步:输出图像  
  22.     }  
  23.     //通过调用该方法获取随机创建的验证码字符串  
  24.     function getCheckCode(){  
  25.         return $this->checkCode;  
  26.     }  
  27.     //创建背景图像  
  28.     private function createImage(){  
  29.         $this->image=imagecreatetruecolor($this->width, $this->height);  
  30.         //随机背景色  
  31.         $backColor=imagecolorallocate($this->image, rand(225,255), rand(225,255), rand(225,255));  
  32.         //为背景填充颜色  
  33.         imagefill($this->image, 0, 0, $backColor);  
  34.         //设置边框颜色  
  35.         $border=imagecolorallocate($this->image, 0, 0, 0);  
  36.         //画出矩形边框  
  37.         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);    
  38.     }  
  39.     //输出干扰元素  
  40.     private function setDisturbColor() {  
  41.         $lineNum=rand(2,4);               //设置干扰线数量  
  42.         for($i=0;$i<$lineNum;$i++) {  
  43.             $x1=rand(0,$this->width/2);  
  44.             $y1=rand(0,$this->height/2);  
  45.             $x2=rand($this->width/2,$this->width);  
  46.             $y2=rand($this->height/2,$this->height);  
  47.             $color=imagecolorallocate($this->image, rand(100,200), rand(100,200), rand(100,200));   //颜色设置比背景深,比文字浅  
  48.             imageline($this->image, $x1$y1$x2$y2$color);  
  49.         }  
  50.     }  
  51.     //生成验证码字符串  
  52.     private function createCheckCode() {    //或者这里可以通过前台传递过来的参数生成字符  
  53.         $code="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  54.         $string="";  
  55.         for($i=0;$i<$this->codeNum;$i++) {  
  56.             $char=$code{rand(0,strlen($code)-1)};  
  57.             $string.=$char;  
  58.         }  
  59.         return $string;  
  60.     }  
  61.     //输出验证码  
  62.     private function outputText() {  
  63.         //echo "<script type='text/javascript'>alert('".$this->checkCode."')</script>";  
  64.         $string=$this->checkCode;  
  65.         for($i=0;$i<$this->codeNum;$i++) {  
  66.             $x=rand(1,4)+$this->width*$i/$this->codeNum;  
  67.             $y=rand(1,$this->height/4);  
  68.             $color=imagecolorallocate($this->image, rand(0,128), rand(0,128), rand(0,128));  
  69.             $fontSize=rand(4,5);  
  70.             imagestring($this->image, $fontSize$x$y$string[$i], $color);  
  71.         }     
  72.     }  
  73.     //输出图像  
  74.     private function outputImage() {  
  75.         if(imagetypes() & IMG_GIF) {  
  76.             header("Content-type:image/gif");  
  77.             imagepng($this->image);  
  78.         }else if(imagetypes() & IMG_JPG) {  
  79.             header("Content-type:image/jpeg");  
  80.             imagepng($this->image);  
  81.         }else if(imagetypes() & IMG_PNG) {  
  82.             header("Content-type:image/png");  
  83.             imagepng($this->image);  
  84.         }else if(imagetypes() & IMG_WBMP) {  
  85.             header("Content-type:image/vnd.wap.wbmp");  
  86.             imagepng($this->image);  
  87.         }else {  
  88.             die("PHP不支持图像创建");  
  89.         }  
  90.     }  
  91.     function __destruct() {  
  92.         imagedestroy($this->image);  
  93.     }  
  94. }  
  95. $code=new ValidationCode(60, 20, 4);  
  96. $_SESSION['checkCode']=$code->getCheckCode();     //将验证码的值存入session中以便在页面中调用验证  
  97. $code->showImage();   //输出验证码  
  98. ?>  

checkCode.php

和index.php结合以验证输入的正确性,并用Ajax改变前端显示

  1. <?php  
  2.     session_start();  
  3.     //注意如此此处存在中文验证码时,用Ajax传值要注意存在中文乱码的问题  
  4.     $validateCode=$_POST['validateCode'];  
  5.     if(strtoupper($validateCode)==strtoupper($_SESSION['checkCode']))          //判断文本框中输入的值和$_SESSION中保存的验证码值是否相等  
  6.         echo "1";           
  7.     else   
  8.         echo "0";  
  9. ?> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值