


php implements a dynamic random verification code mechanism (CAPTCHA), verification code captcha
php implements a dynamic random verification code mechanism
The verification code (CAPTCHA) is "Completely The abbreviation of "Automated Public Turing test to tell Computers and Humans Apart" is a public, fully automated program that distinguishes whether the user is a computer or a human. It can prevent: malicious cracking of passwords, ticket fraud, forum flooding, and effectively prevents a hacker from using a specific program to violently crack a specific registered user from making continuous login attempts. In fact, using verification codes is a common method for many websites now. We use This function is implemented in a relatively simple way.
This question can be generated and judged by a computer, but it must only be answered by a human. Since computers cannot answer CAPTCHA questions, the user who answers the questions can be considered a human.
The dynamic verification code produced by Php is based on the image processing of PHP. Let’s first introduce the image processing of PHP.
1. Introduction to php image processing
In PHP5, the processing of dynamic images is much easier than before. PHP5 includes the GD extension package in the php.ini file. You only need to remove the corresponding comments of the GD extension package to use it normally. The GD library included in PHP5 is the upgraded GD2 library, which contains some useful JPG functions that support true color image processing.
Generally generated graphics are stored in PHP document format, but dynamic graphics can be directly obtained through HTML image insertion method SRC. For example, verification code, watermark, thumbnail, etc.
General process of creating an image:
<p><strong>1).设定标头,告诉浏览器你要生成的MIME类型。</strong></p> <p><strong>2).创建一个图像区域,以后的操作都将基于此图像区域。</strong></p> <p><strong>3).在空白图像区域绘制填充背景。</strong></p> <p><strong>4).在背景上绘制图形轮廓输入文本。</strong></p> <p><strong>5).输出最终图形。</strong></p> <p><strong>6).清除所有资源。</strong></p> <p><strong>7).其他页面调用图像。</strong></p>
The first step is to set the file MIME type and the output type. Change the output type to image stream
<p>header('Content-Type: image/png;');</p>
General generation The image can be png, jpeg, gif, wbmp
The second step is to create a graphics area, image background
imagecreatetruecolor() returns an image identifier symbol, representing a black image with sizes x_size
and y_size
. Syntax: resource imagecreatetruecolor ( int $width
, int $height
)
<p>$im = imagecreatetruecolor(200,200);</p>
The third step, in the blank image area Draw a filled background
requires a color filler; imagecolorallocate -- assign a color to an image; Syntax: int imagecolorallocate ( resource $image
, int $red
, int $green
, int $blue
)
<p>$blue = imagecolorallocate($im,0,102,255);</p>
Fill this blue color into the background; imagefill -- area filling; Syntax: bool imagefill ( resource $image
, int $x
, int $y
, int $color
)
<p>imagefill($im,0,0,$blue);</p>
Step 4 , enter some lines, text, etc. on the blue background
Color Filler
<p>$white = imagecolorallocate($im,255,255,255);</p>
Draw two line segments: imageline
imageline() Draw a line in the image color
using the image
color from the coordinates x1
, y1
to x2
, y2
(the upper left corner of the image is 0, 0) line segment. Syntax: bool imageline ( resource $image
, int $x1
, int $y1
, int $x2
, int $y2
, int $color
)
<p>imageline($im,0,0,200,200,$white);</p> <p>imageline($im,200,0,0,200,$white);</p>
Draw a line of string horizontally: imagestring
imagestring() Draw the string col
to s color > image
, x
coordinates of the image represented (this is the coordinate of the upper left corner of the string, the upper left corner of the entire image is 0, 0). If y
is 1, 2, 3, 4 or 5, the built-in font is used. font
Syntax: bool imagestring ( resource , int $image
, int $font
, int $x
, string $y
, int $s
)$col
<p>imagestring($im,5,66,20,'jingwhale',$white);</p>The fifth step, output the final graphics
imagepng() Output the GD image stream () to the standard output in PNG format (usually a browser), or output to a filename if given with image
. filename
Syntax: bool imagepng ( resource [, string $image
] )$filename
<p>imagepng($im);</p>The sixth step, I want to combine all Clear all resources
imagedestroy() Release the memory associated with . image
Syntax: bool imagedestroy ( resource )$image
<p>imagedestroy($im);</p>Graphics created by calling other pages (html)
<p><img src="/static/imghwm/default1.png" data-src="Demo4.php" class="lazy" alt="PHP创建的图片" /></p>The sample code is as follows:
<?<span>php </span><span>//</span><span>第一步,设置文件MIME类型</span> <span>header</span>('Content-Type: image/png;'<span>); </span><span>//</span><span>第二步,创建一个图形区域,图像背景</span> <span>$im</span> = imagecreatetruecolor(200,200<span>); </span><span>//</span><span>第三步,在空白图像区域绘制填充背景</span> <span>$blue</span> = imagecolorallocate(<span>$im</span>,0,102,255<span>); imagefill(</span><span>$im</span>,0,0,<span>$blue</span><span>); </span><span>//</span><span>第四步,在蓝色的背景上输入一些线条,文字等</span> <span>$white</span> = imagecolorallocate(<span>$im</span>,255,255,255<span>); imageline(</span><span>$im</span>,0,0,200,200,<span>$white</span><span>); imageline(</span><span>$im</span>,200,0,0,200,<span>$white</span><span>); imagestring(</span><span>$im</span>,5,66,20,'Jing.Whale',<span>$white</span><span>); </span><span>//</span><span>第五步,输出最终图形</span> imagepng(<span>$im</span><span>); </span><span>//</span><span>第六步,我要将所有的资源全部清空</span> imagedestroy(<span>$im</span><span>); </span>?>Display effect:
2. Create dynamic verification code
Attach: Code source addresshttps://github.com/cnblogs-/php-captcha
1. 创建带验证码的图片,并模糊背景
随机码采用16进制;模糊背景即在图片背景加上线条、雪花等。
1)创建随机码
<span>for</span> (<span>$i</span>=0;<span>$i</span><<span>$_rnd_code</span>;<span>$i</span>++<span>) { </span><span>$_nmsg</span> .= <span>dechex</span>(<span>mt_rand</span>(0,15<span>)); }</span>
string dechex ( int $number
),返回一字符串,包含有给定 number
参数的十六进制表示。
2)保存在session
<span>$_SESSION</span>['code'] = <span>$_nms</span>
3)创建图片
<span>//</span><span>创建一张图像</span> <span>$_img</span> = imagecreatetruecolor(<span>$_width</span>,<span>$_height</span><span>); </span><span>//</span><span>白色</span> <span>$_white</span> = imagecolorallocate(<span>$_img</span>,255,255,255<span>); </span><span>//</span><span>填充</span> imagefill(<span>$_img</span>,0,0,<span>$_white</span><span>); </span><span>if</span> (<span>$_flag</span><span>) { </span><span>//</span><span>黑色,边框</span> <span>$_black</span> = imagecolorallocate(<span>$_img</span>,0,0,0<span>); imagerectangle(</span><span>$_img</span>,0,0,<span>$_width</span>-1,<span>$_height</span>-1,<span>$_black</span><span>); }</span>
4)模糊背景
<span>//</span><span>随机画出6个线条</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><6;<span>$i</span>++<span>) { </span><span> $_rnd_color</span> = imagecolorallocate(<span>$_img</span>,<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255<span>)); imageline(</span><span>$_img</span>,<span>mt_rand</span>(0,<span>$_width</span>),<span>mt_rand</span>(0,<span>$_height</span>),<span>mt_rand</span>(0,<span>$_width</span>),<span>mt_rand</span>(0,<span>$_height</span>),<span>$_rnd_color</span><span>); } </span><span>//随机</span><span>雪花</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>) { </span><span>$_rnd_color</span> = imagecolorallocate(<span>$_img</span>,<span>mt_rand</span>(200,255),<span>mt_rand</span>(200,255),<span>mt_rand</span>(200,255<span>)); imagestring(</span><span>$_img</span>,1,<span>mt_rand</span>(1,<span>$_width</span>),<span>mt_rand</span>(1,<span>$_height</span>),'*',<span>$_rnd_color</span><span>); }</span>
5)输出及销毁
<span>//</span><span>输出验证码</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><<span>strlen</span>(<span>$_SESSION</span>['code']);<span>$i</span>++<span>) { </span><span>$_rnd_color</span> = imagecolorallocate(<span>$_img</span>,<span>mt_rand</span>(0,100),<span>mt_rand</span>(0,150),<span>mt_rand</span>(0,200<span>)); imagestring(</span><span>$_img</span>,5,<span>$i</span>*<span>$_width</span>/<span>$_rnd_code</span>+<span>mt_rand</span>(1,10),<span>mt_rand</span>(1,<span>$_height</span>/2),<span>$_SESSION</span>['code'][<span>$i</span>],<span>$_rnd_color</span><span>); } </span><span>//</span><span>输出图像</span> <span>header</span>('Content-Type: image/png'<span>); imagepng(</span><span>$_img</span><span>); </span><span>//</span><span>销毁</span> imagedestroy(<span>$_img</span>);
将其封装在global.func.php全局函数库中,函数名为_code(),以便调用。我们将设置$_width ,$_height ,$_rnd_code,$_flag 四个参数,以增强函数的灵活性。
<p>* @param <strong>int $_width</strong> 验证码的长度:如果要6位长度推荐75+50;如果要8位,推荐75+50+50,依次类推<br>* @param <strong>int $_height</strong> 验证码的高度<br>* @param <strong>int $_rnd_code</strong> 验证码的位数<br>* @param <strong>bool $_flag</strong> 验证码是否需要边框:true有边框, false无边框(默认)</p>
封装后的代码如下:
<?<span>php </span><span>/*</span><span>* * [verification-code] (C)2015-2100 jingwhale. * * This is a freeware * $Id: global.func.php 2015-02-05 20:53:56 jingwhale$ </span><span>*/</span>
<span>/*</span><span>* * _code()是验证码函数 * @access public * @param int $_width 验证码的长度:如果要6位长度推荐75+50;如果要8位,推荐75+50+50,依次类推 * @param int $_height 验证码的高度 * @param int $_rnd_code 验证码的位数 * @param bool $_flag 验证码是否需要边框:true有边框, false无边框(默认) * @return void 这个函数执行后产生一个验证码 </span><span>*/</span> <span>function</span> _code(<span>$_width</span> = 75,<span>$_height</span> = 25,<span>$_rnd_code</span> = 4,<span>$_flag</span> = <span>false</span><span>) { </span><span>//</span><span>创建随机码</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><<span>$_rnd_code</span>;<span>$i</span>++<span>) { </span><span>$_nmsg</span> .= <span>dechex</span>(<span>mt_rand</span>(0,15<span>)); } </span><span>//</span><span>保存在session</span> <span>$_SESSION</span>['code'] = <span>$_nmsg</span><span>; </span><span>//</span><span>创建一张图像</span> <span>$_img</span> = imagecreatetruecolor(<span>$_width</span>,<span>$_height</span><span>); </span><span>//</span><span>白色</span> <span>$_white</span> = imagecolorallocate(<span>$_img</span>,255,255,255<span>); </span><span>//</span><span>填充</span> imagefill(<span>$_img</span>,0,0,<span>$_white</span><span>); </span><span>if</span> (<span>$_flag</span><span>) { </span><span>//</span><span>黑色,边框</span> <span>$_black</span> = imagecolorallocate(<span>$_img</span>,0,0,0<span>); imagerectangle(</span><span>$_img</span>,0,0,<span>$_width</span>-1,<span>$_height</span>-1,<span>$_black</span><span>); } </span><span>//</span><span>随即画出6个线条</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><6;<span>$i</span>++<span>) { </span><span>$_rnd_color</span> = imagecolorallocate(<span>$_img</span>,<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255<span>)); imageline(</span><span>$_img</span>,<span>mt_rand</span>(0,<span>$_width</span>),<span>mt_rand</span>(0,<span>$_height</span>),<span>mt_rand</span>(0,<span>$_width</span>),<span>mt_rand</span>(0,<span>$_height</span>),<span>$_rnd_color</span><span>); } </span><span>//</span><span>随即雪花</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>) { </span><span>$_rnd_color</span> = imagecolorallocate(<span>$_img</span>,<span>mt_rand</span>(200,255),<span>mt_rand</span>(200,255),<span>mt_rand</span>(200,255<span>)); imagestring(</span><span>$_img</span>,1,<span>mt_rand</span>(1,<span>$_width</span>),<span>mt_rand</span>(1,<span>$_height</span>),'*',<span>$_rnd_color</span><span>); } </span><span>//</span><span>输出验证码</span> <span>for</span> (<span>$i</span>=0;<span>$i</span><<span>strlen</span>(<span>$_SESSION</span>['code']);<span>$i</span>++<span>) { </span><span>$_rnd_color</span> = imagecolorallocate(<span>$_img</span>,<span>mt_rand</span>(0,100),<span>mt_rand</span>(0,150),<span>mt_rand</span>(0,200<span>)); imagestring(</span><span>$_img</span>,5,<span>$i</span>*<span>$_width</span>/<span>$_rnd_code</span>+<span>mt_rand</span>(1,10),<span>mt_rand</span>(1,<span>$_height</span>/2),<span>$_SESSION</span>['code'][<span>$i</span>],<span>$_rnd_color</span><span>); } </span><span>//</span><span>输出图像</span> <span>header</span>('Content-Type: image/png'<span>); imagepng(</span><span>$_img</span><span>); </span><span>//</span><span>销毁</span> imagedestroy(<span>$_img</span><span>); } </span>?>
2.创建验证机制
创建php验证页面,通过session来检验验证码是否一致。
1)创建verification-code.php验证页面
<?<span>php </span><span>/*</span><span>* * [verification-code] (C)2015-2100 jingwhale. * * This is a freeware * $Id: verification-code.php 2015-02-05 20:53:56 jingwhale$ </span><span>*/</span> <span>//</span><span>设置字符集编码</span> <span>header</span>('Content-Type: text/html; charset=utf-8'<span>); </span>?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>verification code</title> <link rel="stylesheet" type="text/css" href="style/basic.css" /> </head> <body> <div id="testcode"> <form method="post" name="verification" action="verification-code.php?action=verification"> <<span>dl</span>> <dd>验证码:<input type="text" name="code" <span>class</span>="code" /><img src="/static/imghwm/default1.png" data-src="codeimg.php" class="lazy" id="codeimg" / alt="PHP implements dynamic random verification code mechanism (CAPTCHA), verification code captcha_PHP tutorial" ></dd> <dd><input type="submit" <span>class</span>="submit" value="验证" /></dd> </<span>dl</span>> </form> </div> </body> </html>
显示如下:
2)创建产生验证码图片页面
创建codeimg.php为verification-code.php html代码里的img提供验证码图片
首先必须在codeimg.php页面开启session;
其次,将我们封装好的global.func.php全局函数库引入进来;
最后,运行_code();
<?<span>php </span><span>/*</span><span>* * [verification-code] (C)2015-2100 jingwhale. * * This is a freeware * $Id: codeimg.php 2015-02-05 20:53:56 jingwhale$ </span><span>*/</span> <span>//</span><span>开启session</span> <span>session_start</span><span>(); </span><span>//</span><span>引入全局函数库(自定义)</span> <span>require</span> <span>dirname</span>(<span>__FILE__</span>).'/includes/global.func.php'<span>; </span><span>//</span><span>运行验证码函数。通过数据库的_code方法,设置验证码的各种属性,生成图片</span> _code(125,25,6,<span>false</span><span>); </span>?>
3)创建session检验机制
首先必须在verification-code.php页面也开启session;
其次,设计提交验证码的方式,本文以get方式提交,当action=verification时提交成功;
最后,创建验证函数,原理是将客户端用户提交的验证码同服务器codeimg.php中session的验证码是否一致;这里有一个js弹窗函数_alert_back(),我们也把它封装在global.func.php里;
修改verification-code.php中php代码如下:
<?<span>php </span><span>/*</span><span>* * [verification-code] (C)2015-2100 jingwhale. * * This is a freeware * $Id: verification-code.php 2015-02-05 20:53:56 jingwhale$ </span><span>*/</span> <span>//</span><span>设置字符集编码</span> <span>header</span>('Content-Type: text/html; charset=utf-8'<span>); </span><span>//</span><span>开启session</span> <span>session_start</span><span>(); </span><span>//</span><span>引入全局函数库(自定义)</span> <span>require</span> <span>dirname</span>(<span>__FILE__</span>).'/includes/global.func.php'<span>; </span><span>//</span><span>检验验证码</span> <span>if</span> (<span>$_GET</span>['action'] == 'verification'<span>) { </span><span>if</span> (!(<span>$_POST</span>['code'] == <span>$_SESSION</span>['code'<span>])) { _alert_back(</span>'验证码不正确!'<span>); }</span><span>else</span><span>{ _alert_back(</span>'验证码通过!'<span>); } } </span>?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>verification code</title> <link rel="stylesheet" type="text/css" href="style/basic.css" /> <script type="text/javascript"></script> </head> <body> <div id="testcode"> <form method="post" name="verification" action="verification-code.php?action=verification"> <<span>dl</span>> <dd>验证码:<input type="text" name="code" <span>class</span>="code" /><img src="/static/imghwm/default1.png" data-src="codeimg.php" class="lazy" id="codeimg" / alt="PHP implements dynamic random verification code mechanism (CAPTCHA), verification code captcha_PHP tutorial" ></dd> <dd><input type="submit" <span>class</span>="submit" value="验证" /></dd> </<span>dl</span>> </form> </div> </body> </html>
3.实现点击验证码图片更新验证码
上面若想实现验证码更新,必须刷新页面;我们写一个codeimg.js函数实现点击验证码图片更新验证码
window.onload = <span>function</span><span> () { </span><span>var</span> code = document.getElementById('codeimg');<span>//</span><span>通过id找到html中img标签</span> code.onclick = <span>function</span> () {<span>//</span><span>为标签添加点击事件</span> <span>this</span>.src='codeimg.php?tm='+Math.random();<span>//</span><span>修改时间,重新指向codeimg.php</span> <span> }; }</span>
然后在verification-code.php html代码head里它即可。
完
欢迎转载。转载请注明转载字样,标注原作者和原博文地址。

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

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

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

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

判断方法: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

SublimeText3 Chinese version
Chinese version, very easy to use

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools