首頁 > php教程 > php手册 > PHP制作验证码,php验证码

PHP制作验证码,php验证码

WBOY
發布: 2016-06-13 09:24:21
原創
986 人瀏覽過

PHP制作验证码,php验证码

PHP制作验证码详细教程

效果:

myvcode.class.php:封装创建验证码的类

<span><span id="lnum1">   1:</span> <?php</span>
登入後複製
登入後複製
<span><span id="lnum2">   2:</span> <span>/*</span></span>
登入後複製
<span><span id="lnum3">   3:</span> <span>* file:myvcode.class.php</span></span>
登入後複製
<span><span id="lnum4">   4:</span> <span>* 验证码类,类名Vcode</span></span>
登入後複製
<span><span id="lnum5">   5:</span> <span>*/</span></span>
登入後複製
<span><span id="lnum6">   6:</span> <span>class</span> Vcode</span>
登入後複製
<span><span id="lnum7">   7:</span> {</span>
登入後複製
<span><span id="lnum8">   8:</span>     <span>private</span> $width;              <span>/*验证码宽度*/</span></span>
登入後複製
<span><span id="lnum9">   9:</span>     <span>private</span> $height;             <span>/*验证码高度*/</span></span>
登入後複製
<span><span id="lnum10">  10:</span>     <span>private</span> $codeNum;            <span>/*验证码字符个数*/</span></span>
登入後複製
<span><span id="lnum11">  11:</span>     <span>private</span> $checkCode;            <span>/*验证码字符*/</span></span>
登入後複製
<span><span id="lnum12">  12:</span>     <span>private</span> $image;                <span>/*验证码资源*/</span></span>
登入後複製
<span><span id="lnum13">  13:</span>     <span>private</span> $pixNum;            <span>/*绘制干扰点的个数*/</span></span>
登入後複製
<span><span id="lnum14">  14:</span>     <span>private</span> $lineNum;            <span>/*绘制干扰线的条数*/</span></span>
登入後複製
<span><span id="lnum15">  15:</span></span>
登入後複製
<span><span id="lnum16">  16:</span>     <span>/*</span></span>
登入後複製
<span><span id="lnum17">  17:</span> <span>    *构造方法实例化验证码对象,并初始化数据</span></span>
登入後複製
<span><span id="lnum18">  18:</span> <span>    *@param int $width         设置默认宽度</span></span>
登入後複製
<span><span id="lnum19">  19:</span> <span>    *@param int $height     设置默认高度</span></span>
登入後複製
<span><span id="lnum20">  20:</span> <span>    *@param int $codeNum    设置验证码中的字符个数</span></span>
登入後複製
<span><span id="lnum21">  21:</span> <span>    *@param int $pixNum        设置干扰点的个数</span></span>
登入後複製
<span><span id="lnum22">  22:</span> <span>    *@param int $lineNum    设置干扰线的数量</span></span>
登入後複製
<span><span id="lnum23">  23:</span> <span>    */</span></span>
登入後複製
<span><span id="lnum24">  24:</span>     <span>function</span> __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)</span>
登入後複製
<span><span id="lnum25">  25:</span>     {</span>
登入後複製
<span><span id="lnum26">  26:</span>         $this->width = $width;</span>
登入後複製
<span><span id="lnum27">  27:</span>         $this->height = $height;</span>
登入後複製
<span><span id="lnum28">  28:</span>         $this->codeNum = $codeNum;</span>
登入後複製
<span><span id="lnum29">  29:</span>         $this->pixNum = $pixNum;</span>
登入後複製
<span><span id="lnum30">  30:</span>         $this->lineNum = $lineNum;</span>
登入後複製
<span><span id="lnum31">  31:</span>     }</span>
登入後複製
<span><span id="lnum32">  32:</span>     <span>/*内部私有方法,创建图像资源*/</span></span>
登入後複製
<span><span id="lnum33">  33:</span>     <span>private</span> <span>function</span> getCreateImage()</span>
登入後複製
<span><span id="lnum34">  34:</span>     {</span>
登入後複製
<span><span id="lnum35">  35:</span>         $this->image = imagecreatetruecolor($this->width, $this->height);</span>
登入後複製
<span><span id="lnum36">  36:</span>         $white = imagecolorallocate($this->image,0xff,0xff,0xff);</span>
登入後複製
<span><span id="lnum37">  37:</span>         imagefill($this->image, 0, 0, $white);</span>
登入後複製
<span><span id="lnum38">  38:</span>         $black = imagecolorallocate($this->image,0,0,0);</span>
登入後複製
<span><span id="lnum39">  39:</span>         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);</span>
登入後複製
<span><span id="lnum40">  40:</span>     }</span>
登入後複製
<span><span id="lnum41">  41:</span>     <span>/*内部私有方法,绘制字符,去掉o0Llz和012*/</span></span>
登入後複製
<span><span id="lnum42">  42:</span>     <span>private</span> <span>function</span> createCheckCode()</span>
登入後複製
<span><span id="lnum43">  43:</span>     {</span>
登入後複製
<span><span id="lnum44">  44:</span>         $code = <span>'3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY'</span>;</span>
登入後複製
<span><span id="lnum45">  45:</span>         $this->checkCode = <span>""</span>;</span>
登入後複製
<span><span id="lnum46">  46:</span>         <span>for</span>($i=0; $i<$this->codeNum;$i++)</span>
登入後複製
<span><span id="lnum47">  47:</span>         {</span>
登入後複製
<span><span id="lnum48">  48:</span>             $char = $code{rand(0,strlen($code) - 1)};</span>
登入後複製
<span><span id="lnum49">  49:</span>             $this->checkCode .= $char;</span>
登入後複製
<span><span id="lnum50">  50:</span>             $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));</span>
登入後複製
<span><span id="lnum51">  51:</span>             $fontSize = rand(3,5);</span>
登入後複製
<span><span id="lnum52">  52:</span>             $x = rand(0,$this->width-imagefontwidth($fontSize));</span>
登入後複製
<span><span id="lnum53">  53:</span>             $y = rand(0,$this->height-imagefontheight($fontSize));</span>
登入後複製
<span><span id="lnum54">  54:</span>             imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);</span>
登入後複製
<span><span id="lnum55">  55:</span>         }</span>
登入後複製
<span><span id="lnum56">  56:</span>     }</span>
登入後複製
<span><span id="lnum57">  57:</span>     <span>/*内部私有方法设置干扰元素*/</span></span>
登入後複製
<span><span id="lnum58">  58:</span>     <span>private</span> <span>function</span> setDisturbColor()</span>
登入後複製
<span><span id="lnum59">  59:</span>     {</span>
登入後複製
<span><span id="lnum60">  60:</span>         <span>/*绘制干扰点*/</span></span>
登入後複製
<span><span id="lnum61">  61:</span>         <span>for</span>($i=0; $i<$this->pixNum; $i++)</span>
登入後複製
<span><span id="lnum62">  62:</span>         {</span>
登入後複製
<span><span id="lnum63">  63:</span>             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));</span>
登入後複製
<span><span id="lnum64">  64:</span>             imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);</span>
登入後複製
<span><span id="lnum65">  65:</span>         }</span>
登入後複製
<span><span id="lnum66">  66:</span>         <span>/*绘制干扰线*/</span></span>
登入後複製
<span><span id="lnum67">  67:</span>         <span>for</span>($i=0; $i<$this->lineNum; $i++)</span>
登入後複製
<span><span id="lnum68">  68:</span>         {</span>
登入後複製
<span><span id="lnum69">  69:</span>             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));</span>
登入後複製

70: imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),

<p><span>rand($this->width / 2,$this->width &ndash; 2), rand($this->height / 2,$this->height &ndash; 2), $color);</span></p>
登入後複製
<span><span id="lnum71">  71:</span>         }</span>
登入後複製
<span><span id="lnum72">  72:</span>     }</span>
登入後複製
<span><span id="lnum73">  73:</span>     <span>/*开启session保存 利用echo 输出图像*/</span></span>
登入後複製
<span><span id="lnum74">  74:</span>     <span>function</span> __toString()</span>
登入後複製
<span><span id="lnum75">  75:</span>     {</span>
登入後複製
<span><span id="lnum76">  76:</span>         $_SESSION[<span>'code'</span>] = strtoupper($this->checkCode);</span>
登入後複製
<span><span id="lnum77">  77:</span>         $this->getCreateImage();</span>
登入後複製
<span><span id="lnum78">  78:</span>         $this->createCheckCode();</span>
登入後複製
<span><span id="lnum79">  79:</span>         $this->setDisturbColor();</span>
登入後複製
<span><span id="lnum80">  80:</span>         $this->outputImg();</span>
登入後複製
<span><span id="lnum81">  81:</span>     }</span>
登入後複製
<span><span id="lnum82">  82:</span>     <span>/*内部私有方法输出图像*/</span></span>
登入後複製
<span><span id="lnum83">  83:</span>     <span>private</span> <span>function</span> outputImg()</span>
登入後複製
<span><span id="lnum84">  84:</span>     {</span>
登入後複製
<span><span id="lnum85">  85:</span>         header(<span>"content-type:image/png"</span>);</span>
登入後複製
<span><span id="lnum86">  86:</span>         imagepng($this->image);</span>
登入後複製
<span><span id="lnum87">  87:</span>     }</span>
登入後複製
<span><span id="lnum88">  88:</span>     <span>/*析构方法,释放对象*/</span></span>
登入後複製
<span><span id="lnum89">  89:</span>     <span>function</span> __destruct()</span>
登入後複製
<span><span id="lnum90">  90:</span>     {</span>
登入後複製
<span><span id="lnum91">  91:</span>         imagedestroy($this->image);</span>
登入後複製
<span><span id="lnum92">  92:</span>     }</span>
登入後複製
<span><span id="lnum93">  93:</span> }</span>
登入後複製
<span><span id="lnum94">  94:</span> ?></span>
登入後複製

imgcode.php输出图像

<span><span id="lnum1">   1:</span> <?php</span>
登入後複製
登入後複製
<span><span id="lnum2">   2:</span> session_start();</span>
登入後複製
<span><span id="lnum3">   3:</span> <span>require_once</span>(<span>'myvcode.class.php'</span>);</span>
登入後複製
<span><span id="lnum4">   4:</span> <span>echo</span> <span>new</span> Vcode();</span>
登入後複製
<span><span id="lnum5">   5:</span> ?></span>
登入後複製

test.html:同过img标签引用

<span><span id="lnum1">   1:</span> <span><</span><span>img</span> <span>src</span><span>="imgcode.php"</span><span>></span></span>
登入後複製

可以加一个a标签,用js实现换一张效果:

<span>/*局部刷新换验证码*/
function changeCode()
{
	var imgcode = document.getElementById('code');
	var change = document.getElementById('change');
	change.onclick = function()
	{
		/*必须加后面的参数才能刷新*/
		imgcode.src='code.php?tm'+Math.random();
	}
}</span>
登入後複製


code和change分别是img和a的id

 

PHP验证码制作的问题

很简单,你用[pic]时PHP会检查是否有一个名为pic的常量。这肯定找不到,于是它就在文件开头输出一个警告信息。此信息在图片数据前输出,导致图片无法被浏览器识别。解决方法是给pic加引号,$_SESSION['pic']
 

php图片验证码实现

  可以用php的GD库做

  //随机生成验证码
  class randomString
  {

  function createRandomStr($strLen)
  {
  list($usec, $sec) = explode(' ', microtime());
  (float) $sec + ((float) $usec * 100000);

  $number = '';
  $number_len = $strLen;
  $stuff = '1234567890abcdefghijklmnopqrstuvwxyz';//附加码显示范围ABCDEFGHIJKLMNOPQRSTUVWXYZ
  $stuff_len = strlen($stuff) - 1;
  for ($i = 0; $i   $number .= substr($stuff, mt_rand(0, $stuff_len), 1);
  }
  return $number;
  }
  }
  通过ZD库将验证码变成图片
  $number = $createStr->createRandomStr('4');//验证码的位数
  $number_len = strlen($number);
  $_SESSION["VERIFY_CODE"] = $number;

  // 生成验证码图片
  $img_width = 60;
  $img_height = 20;

  $img = imageCreate($img_width, $img_height);
  ImageColorAllocate($img, 0x6C, 0x74, 0x70);
  $white = ImageColorAllocate($img, 0xff, 0xff, 0xff);

  $ix = 6;
  $iy = 2;
  for ($i = 0; $i   imageString($img, 5, $ix, $iy, $number[$i], $white);
  $ix += 14;
  }
  for($i=0;$i  {
  $randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);
  }

  // 输出图片
  header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));

  imagepng($img);
  imagedestroy($img);...余下全文>>
 

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板