PHP implements dynamic random verification code mechanism_PHP tutorial
WBOY
Release: 2016-07-13 10:07:39
Original
1016 people have browsed it
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 brute force a specific registered user to continuously log in. 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 humans can answer it. Since computers cannot answer CAPTCHA questions, the user who answers the questions can be considered a human.
The production of dynamic verification codes in 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, 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 obtained directly 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.
2). 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.
The first step is to set the file MIME type and output type. Change the output type to image stream
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 from coordinates x1, y1 to x2, y2 (the upper left corner of the image is 0, 0) using color color. Syntax: bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline($im,0,0,200,200,$white);
imageline($im,200,0,0,200,$white);
Draw a line of string horizontally: imagestring
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, and 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 filename is given. Syntax: bool imagepng ( resource $image [, string $filename ] )
imagepng($im);
The sixth step is to clear all resources
imagedestroy() releases the memory associated with image. Syntax: bool imagedestroy ( resource $image )
imagedestroy($im);
Graphics created by calling other pages (html)
The sample code is as follows:
Copy code
//The first step is to set the file MIME type
header('Content-Type: image/png;');
//The second step is to create a graphics area and image background
$im = imagecreatetruecolor(200,200);
//The third step is to draw a filled background in the blank image area
$blue = imagecolorallocate($im,0,102,255);
imagefill($im,0,0,$blue);
//Step 4, enter some lines, text, etc. on the blue background
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)
& lt; dd & gt; verification code: & lt; input type = "text" name = "code" class = "code" /& gt; & lt; img src = "codeimg.php" id = "codeimg" /& g. t; & lt ;/dd>
& lt; dd & gt; & lt; input type = "submit" class = "submit" value = "verification" /& gt; & lt; /dd & gt;
1
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
Copy code
window.onload = function () {
var code = document.getElementById('codeimg');//Find the img tag in html by id
code.onclick = function () {//Add a click event to the label
this.src='codeimg.php?tm='+Math.random();//Modify time and redirect to codeimg.php
};
}
http://www.bkjia.com/PHPjc/954738.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/954738.htmlTechArticlephp implements dynamic random verification code mechanism verification code (CAPTCHA) is Completely Automated Public Turing test to tell Computers and Humans Apart (Fully automatic Turing test to distinguish computers and humans...
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