


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 .= 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 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);
}
// Output image
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($img);
Imagedestroy($img);...The rest of the text>>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
