


PHP production verification code, php verification code_PHP tutorial
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 – 2), rand($this->height / 2,$this->height – 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
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']
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 < $number_len; $i++) {
$number .= substr($stuff, mt_rand(0, $stuff_len), 1);
}
return $number;
}
}
Use the ZD library to turn the verification code into a picture
$number = $createStr->createRandomStr('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>>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
