Home >Backend Development >PHP Tutorial >Verification code class and usage in PHP_PHP tutorial

Verification code class and usage in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:48748browse

Note: Add the public page to session_start();

session_start();

ini_set('display_errors', 'Off');

class CCheckCodeFile

{

//Verification code digits www.2cto.com

var $mCheckCodeNum = 4;

//Generated verification code

var $mCheckCode = '';

//Verification code picture

      var $mCheckImage = '';

//Interfering pixels

      var $mDisturbColor = '';

//Verification code image width

      var $mCheckImageWidth = '80';

//Verification code image width

      var $mCheckImageHeight = '20';

//Output header

        function OutFileHeader()

          {

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

}

//Generate verification code

        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;

}

//Generate verification code image

Function CreateImage()

          {

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

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

                 return $this->mCheckImage;

}

//Set the interference pixels of the image

       function SetDisturbColor()

          {

for ($i=0;$i<=128;$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);

              }

}

//Set the size, width and height of the verification code image

        function SetCheckImageWH($width,$height)

          {

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

                     $this->mCheckImageWidth = $width;

                     $this->mCheckImageHeight = $height;

                return true;

}

//Draw the verification codes one by one on the verification code picture

Function WriteCheckCodeToImage()

          {

for ($i=0;$i<=$this->mCheckCodeNum;$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);

              }

}

//Output verification code image

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);//Set the size of the verification code image

$c_check_code_image->OutCheckImage();

?>

How to use:

          Verification code

        

The picture is not clear? Click to get the verification code again

Verify it is correct

session_start();

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

//       echo("Verification code is correct");

}else{

echo "

                                                                                                                         

                                                                                                                                                                                                                 

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; //产生...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn