


php implements dynamic random verification code mechanism
CAPTCHA is the abbreviation of "Completely Automated Public Turing test to tell Computers and Humans Apart". It 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 only a human can answer it. Since computers cannot answer CAPTCHA questions, the user who answers the questions can be considered a human.
PHP production dynamic verification code is based on PHP image processing. Let's first introduce the image processing of PHP.Introduction to .php image processing
In PHP5, processing 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's document format, but dynamic graphics can be directly obtained through HTML's image insertion method SRC. For example, verification code, watermark, thumbnail, etc.
General process for creating images:
1). Set the header to tell the browser the MIME type you want to generate.The first step is to set the file MIME type and output type. Change the output type to image stream2). Create an image area, and all subsequent operations will be based on this image area.
3).Draw a filled background in the blank image area.
4). Draw graphic outlines on the background to enter text.
5).Output the final graphics.
6). Clear all resources.
7). Call images from other pages.
header('Content-Type: image/png;');Generally generated images can be png, jpeg, gif, wbmp
The second step is to create a graphics area and image background
imagecreatetruecolor() returns an image identifier representing a black image of size x_size and
y_size.
Syntax: resource imagecreatetruecolor ( int $width , int
$height )
$im = imagecreatetruecolor(200,200);The third step is to draw a filled background in the blank image area
Requires a color filler; imagecolorallocate -- assigns a color to an image; Syntax: int imagecolorallocate ( resource $image , int
$red , int
$green , int
$blue )
$blue = imagecolorallocate($im,0,102,255);
Fill this blue color into the background; imagefill -- area filling; Syntax: bool imagefill ( resource $image , int
$x , int
$y , int
$color )
imagefill($im,0,0,$blue);The fourth step is to enter some lines, text, etc. on the blue background
Color Filler
$white = imagecolorallocate($im,255,255,255);Draw two line segments: imageline
imageline() draws a line segment in the image image using the
color color from coordinates
x1,
y1 to
x2,
y2 (the upper left corner of the image is 0, 0).
Syntax: bool imageline ( resource $image , int
$x1 , int
$y1 , int
$x2 , int
$y2 , int
$color )
imageline($im,0,0,200,200,$white);Draw a line of string horizontally: imagestringimageline($im,200,0,0,200,$white);
imagestring() uses col color to draw the string
s to the
x,
y coordinates of the image represented by
image (this is the coordinate of the upper left corner of the string, the upper left corner of the entire image is 0,0). If
font is 1, 2, 3, 4 or 5, the built-in font is used.
Syntax: bool imagestring ( resource $image , int
$font , int
$x , int
$y , string
$s , int
$col )
imagestring($im,5,66,20,'jingwhale',$white);
Step 5, output the final graphics
imagepng() Outputs a GD image stream (image
) in PNG format to standard output (usually a browser), or to a file if a filename is given with filename
. Syntax: bool imagepng ( resource $image
[, string $filename
] )
imagepng($im);
Step six, I want to clear all resources
imagedestroy() frees the memory associated with image
. Syntax: bool imagedestroy ( resource $image
)
imagedestroy($im);
Call the graphics created by other pages (html)
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
Attached: Code source addresshttps://github.com/cnblogs-/php-captcha
1. Create a picture with verification code and blur the background
The random code uses hexadecimal; the blurred background means adding lines, snowflakes, etc. to the background of the picture.
1) Create random code
<span>for</span> (<span>$i</span>=0;<span>$i</span>$_rnd_code;<span>$i</span>++<span>) { </span><span>$_nmsg</span> .= <span>dechex</span>(<span>mt_rand</span>(0,15<span>)); }</span>
string dechex (int $number
), returns a string containing the hexadecimal representation of the given number
parameter.
2) Save in session
<span>$_SESSION</span>['code'] = <span>$_nms</span>
3)Create pictures
<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) Blur background
<span>//</span><span>随即画出6个线条</span> <span>for</span> (<span>$i</span>=0;<span>$i</span>$i++<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>$i++<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) Output and destruction
<span>//</span><span>输出验证码</span> <span>for</span> (<span>$i</span>=0;<span>$i</span>strlen(<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>);
Encapsulate it in the global.func.php global function library, and the function name is _code() for easy calling. We will set the four parameters $_width, $_height, $_rnd_code, $_flag to enhance the flexibility of the function.
* @param int $_width The length of the verification code: if you want 6 digits, 75+50 is recommended; if you want 8 digits, 75+50+50 is recommended, and so on
* @param int $_height The height of the verification code
* @ param int $_rnd_code The number of digits in the verification code
* @param bool $_flag Whether the verification code requires a border: true with border, false without border (default)
The encapsulated code is as follows:
<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>$_rnd_code;<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>$i++<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>$i++<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>strlen(<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. Create a verification mechanism
Create a PHP verification page and check whether the verification code is consistent through session.
1) Create verification-code.php verification page
<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>?> <meta charset="UTF-8"> <title>verification code</title> <link rel="stylesheet" type="text/css" href="style/basic.css"> <div> <form method="post" name="verification" action="verification-code.php?action=verification"> dl> <dd>验证码:<input type="text" name="code">class="code" /><img src="/static/imghwm/default1.png" data-src="codeimg.php" class="lazy" alt="PHP implements dynamic random verification code mechanism (CAPTCHA)" > </dd> <dd> <input type="submit">class="submit" value="验证" /></dd> <span>dl</span>> </form> </div>
Displayed as follows:
2) Create a page to generate verification code
Create codeimg.php to provide verification code images for img in verification-code.php html code
First you must open the session on the codeimg.php page;
Secondly, introduce our encapsulated global.func.php global function library;
Finally, run_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) Create session verification mechanism
First, you must open the session on the verification-code.php page;
Secondly, design the way to submit the verification code. This article is submitted in the get method. When action=verification, the submission is successful;
Finally, create a verification function. The principle is to check whether the verification code submitted by the client user is consistent with the verification code of the session in the server codeimg.php; there is a js pop-up function _alert_back(), and we also encapsulate it in global. in func.php;
Modify the php code in verification-code.php as follows:
<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>?> <meta charset="UTF-8"> <title>verification code</title> <link rel="stylesheet" type="text/css" href="style/basic.css"> <script type="text/javascript" src="js/codeimg.js"></script> <div> <form method="post" name="verification" action="verification-code.php?action=verification"> dl> <dd>验证码:<input type="text" name="code">class="code" /><img src="/static/imghwm/default1.png" data-src="codeimg.php" class="lazy" alt="PHP implements dynamic random verification code mechanism (CAPTCHA)" > </dd> <dd> <input type="submit">class="submit" value="验证" /></dd> <span>dl</span>> </form> </div>
3. Click on the verification code image to update the verification code
If you want to update the verification code above, you must refresh the page; we write a codeimg.js function to update the verification code by clicking on the verification code image
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>
Then it in the verification-code.php html code head.
End
Reprinting is welcome. When reprinting, please indicate the word reprint, the original author and the original blog post address.
The above introduces the implementation of dynamic random verification code mechanism (CAPTCHA) in PHP, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

在使用Golang进行开发或学习过程中,我们可能会遇到undefined:rand.Seed的错误提示。这个错误通常会在需要使用随机数生成器时出现,因为在Golang中需要先设置一个随机数种子,才能使用rand包中的函数。本篇文章将介绍如何解决这种错误。1.引入math/rand包首先,我们需要在代码中引入math/rand包。在


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

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
