Home >Backend Development >PHP Tutorial >PHP production verification code, php verification code_PHP tutorial

PHP production verification code, php verification code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:411089browse

PHP production verification code, php verification code

PHP production verification code detailed tutorial

Effect:

myvcode.class.php: encapsulates the class for creating verification codes

<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 output image

<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: Same as img tag reference

<span><span id="lnum1">   1:</span> <span><</span><span>img</span> <span>src</span><span>="imgcode.php"</span><span>></span></span>

You can add an a tag and use js to achieve the effect of changing one picture:

<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 and change are the id of img and a respectively

Problems with PHP verification code production

It's very simple. When you use [pic], PHP will check whether there is a constant named pic. This is definitely not found, so it outputs a warning message at the beginning of the file. This information is output before the image data, causing the image to not be recognized by the browser. The solution is to add quotes to pic, $_SESSION['pic']

php image verification code implementation

You can use the GD library of php to do it

// Randomly generate verification code
class randomString
{

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

 $number = '';
$ NUMBER_LEN = $ Strlen;
$ Stuff = '1234567890ABCDEFGHIJKLMNOPQRSNOPQRSTUVWXYZ'; // The additional code display range ABCDEFGHIJKLMNOPQRSTUVWXYZ
$ Stuf f_len = strlen ($ stuff) -1;
for ($ i = 0; $i 4eccd40ea9ddcdbdac0049b9b9fc49f4createRandomStr('4');//The number of digits in the verification code
 $number_len = strlen($number );
$_SESSION["VERIFY_CODE"] = $number;

// Generate verification code image
$img_width = 60; $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 < $number_len; $i++) {
imageString($img, 5, $ix, $iy , $number[$i], $white);
 $ix += 14;
 }
 for($i=0;$i<200;$i++) //Add interference pixels
 {
 $randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
 imagesetpixel($img, rand()%100, rand()%50, $randcolor);
 }

// Output image
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));

imagepng($img);
Imagedestroy($img);...The rest of the text>>



http://www.bkjia.com/PHPjc/890823.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/890823.htmlTechArticlePHP production verification code, php verification code PHP production verification code detailed tutorial effect: myvcode.class.php: encapsulation creation Verification code class 1: ?php 2: /* 3: * file:myvcode.class.php 4: * Verification code class...
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