> 백엔드 개발 > PHP 튜토리얼 > PHP中验证码类及使用方法_PHP教程

PHP中验证码类及使用方法_PHP教程

WBOY
풀어 주다: 2016-07-13 17:47:48
원래의
750명이 탐색했습니다.

 

备注:将公共页面加入session_start();

 

session_start();

ini_set('display_errors', 'Off');

class CCheckCodeFile

{

       //验证码位数 www.2cto.com

 

       var $mCheckCodeNum  = 4;

 

       //产生的验证码

       var $mCheckCode   = '';

 

       //验证码的图片

       var $mCheckImage  = '';

 

       //干扰像素

       var $mDisturbColor  = '';

 

       //验证码的图片宽度

       var $mCheckImageWidth = '80';

 

       //验证码的图片宽度

       var $mCheckImageHeight  = '20';

 

       //输出头

       function OutFileHeader()

       {

              header ("Content-type: image/png");

       }

 

       //产生验证码

       function CreateCheckCode()

       {

              //$this->mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));

              $this->mCheckCode = strtoupper(substr(rand(0,9999999999),0,$this->mCheckCodeNum));

              session_cache_expire(60);

              session_start();

              $_SESSION["code_str"] = $this->mCheckCode;

              return $this->mCheckCode;

       }

 

       //产生验证码图片

       function CreateImage()

       {

              $this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight);

              imagecolorallocate ($this->mCheckImage, 255, 255, 255);

              return $this->mCheckImage;

       }

 

       //设置图片的干扰像素

       function SetDisturbColor()

       {

              for ($i=0;$i

              {

                     $this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));

                     imagesetpixel($this->mCheckImage,rand(1,100),rand(1,100),$this->mDisturbColor);

              }

       }

 

       //设置验证码图片的大小\宽\高

       function SetCheckImageWH($width,$height)

       {

              if($width==''||$height=='')return false;

              $this->mCheckImageWidth  = $width;

              $this->mCheckImageHeight = $height;

              return true;

       }

 

       //在验证码图片上逐个画上验证码

       function WriteCheckCodeToImage()

       {

              for ($i=0;$imCheckCodeNum;$i++)

              {

                     $bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));

                     $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;

                     $y = rand(0,$this->mCheckImageHeight-15);

                     imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);

              }

       }

 

       //输出验证码图片

       function OutCheckImage()

       {

              $this ->OutFileHeader();

              $this ->CreateCheckCode();

              $this ->CreateImage();

              $this ->SetDisturbColor();

              $this ->WriteCheckCodeToImage();

              imagepng($this->mCheckImage);

              imagedestroy($this->mCheckImage);

       }

}

 

$c_check_code_image = new CCheckCodeFile();

//$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸

$c_check_code_image->OutCheckImage();

?>

 

 

 

使用方法:

     

验证码

     

      图片看不清?点击重新得到验证码

   

 

 

验证是否正确

 

session_start();

 

  if($_POST["number"] == $_SESSION["code_str"]){

  //      echo("验证码正确");

  }else{

        echo "

 

                     <script></script>

 

                            var s=10;

 

                            function ok()

 

                            {

 

                            s--;

 

                            document.getElementById('tips').innerHTML=s;

 

                            if(s==0)

 

                            {

 

                            clearTimeout(t);

 

                            history.go(-1);

 

                            }

 

                            }

 

                            var t=setInterval('ok()',1000);

 

                    

 

" . "请输入的验证码,请重新输入秒之后,将为您跳转到上一页";

  }

 

摘自 http://itsafe.org/?p=384

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478478.htmlTechArticle备注:将公共页面加入session_start(); ?php session_start(); ini_set(display_errors, Off); class CCheckCodeFile { //验证码位数 www.2cto.com var $mCheckCodeNum = 4; //产生...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿