欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中验证码类及使用方法

程序员文章站 2022-07-11 11:12:53
  备注:将公共页面加入session_start();  

 

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

 

<?php

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<=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);

              }

       }

 

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

       function setcheckimagewh($width,$height)

       {

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

              $this->mcheckimagewidth  = $width;

              $this->mcheckimageheight = $height;

              return true;

       }

 

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

       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);

              }

       }

 

       //输出验证码图片

       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();

?>

 

 

 

使用方法:

<tr>

      <td align="center" valign="middle">验证码</td>

      <td align="center" valign="middle"><input name="number" type="text" style="width:80px;">

      <img src="code.php" id="yzm" onclick="this.src='code.php?abc='+math.random()" alt="图片看不清?点击重新得到验证码" style="cursor:hand;" />

</td>

    </tr>

 

 

验证是否正确

 

session_start();

 

  if($_post["number"] == $_session["code_str"]){

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

  }else{

        echo "

 

                     <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);

 

                     </script>

 

" . "请输入的验证码,请重新输入<span id='tips'></span>秒之后,将为您跳转到<a href=\"javascript:history.go(-1)\">上一页</a>";

  }

 

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