How is the verification code with text pattern in the webpage generated

user:visitors Date:2022-02-09 16:14:26751

When browsing and operating websites, we often encounter verification codes composed of various text characters and line patterns, so how are the verification codes composed of these characters generated?

Here is an example of php code:

First we need to create two pages. A page that displays the captcha for the front end:

Create a blank page named yzm.php and write in it:

<?php

session_start();

if(isset($_POST['yyz'])){

if($_POST["yyz"] == $_SESSION["yyz"]){

echo "Verification code is correct!";

}else{

echo "Verification code error";

}

}

?>

<form action="/" method="post">

<img src="yzm.php"/>

<input name="yyz type="text" "/>

<input type="submit" value="Submit"/>

</form>

The php part is used to accept the submitted verification code, and judge whether it is the same as the verification code passed by the stage variable. If it is the same, the correct operation will be performed. If it is wrong, the verification error will be displayed.

Then create an img image tag, input text box, and submit button respectively.

The form form is used to send verification information, and the yzm.php dynamic address is filled in the image img tag address, which is used to dynamically display the image verification code, the text box is used to input the numbers seen, and the submit button is used to submit the verification parameters.

The next step is to create the yzm.php file used to display dynamic images.

code show as below:

<?php

session_start();

$wenzi = "This is a combination of verification text";

//get the length of characters

$zifuchangdu = mb_strlen($wenzi,"UTF-8");

// convert characters to array

$zifuyanzheng = array();

for( $i=0; $i<$zifuchangdu; $i++){

$zifuyanzheng[$i] = mb_substr($wenzi, $i,1,"UTF-8");

}

$huizhiwenzi = "";

// text on the image

for($i=0; $i<3; $i++){

$suiji=rand(0, 1);

if($suiji==0){

$huizhiwenzi.=$zifuyanzheng[rand(0, $zifuchangdu-1)];

}else{

$huizhiwenzi.=dechex(rand(0,15));

}

}

$yzmimg = imagecreatetruecolor(130, 30);

$ztys = imagecolorallocate($yzmimg, 255,255,255);

imagettftext($yzmimg, 13, rand(2, 9), 20 ,20, $ztys, "FontName.TTF",$huizhiwenzi);

$_SESSION["yyz"] = $huizhiwenzi;//Transfer stage variables

$zft = imagecolorallocate($yzmimg, rand(0, 255),rand(0, 255),rand(0, 255));

// assign random color

for($i=0; $i<180; $i++){

imagesetpixel($yzmimg, rand(1, 130), rand(1, 30), $zft);

}

for($i=0; $i<3; $i++){

imageline($yzmimg, 0, rand(0, 20), rand(70,130), rand(0, 20), $ztys);

}

header("Content-type:image/jpeg");

imagejpeg($yzmimg);

?>

Here we mainly use the GD graphics library in php to generate dynamic pictures.

The GD graphics library provides a comprehensive API for processing pictures. We only need to use the GD library to process or generate pictures. Of course, in addition to the gd library php, there are more powerful Imagick libraries or MagickWandForPHP functions to call ImageMagick software to process and adjust pictures. .

First of all, let's understand some function information used in the following gd library:

imagesetpixel draw pixels

imageline draws lines

imagestring draw character text

imagecreatetruecolor is a new true color image

imagecolorallocate assigns a color to an image

imagejpeg output image

The general principle is to first create a text library for verification, then transfer each text in the text library to an array, and use the rand function to extract a few random characters in the text library as the text of the verification code.

Use the imagecreatetruecolor function to create a picture with a width of 130 and a height of 30px, and imagecolorallocate assigns a background color to it

imagettftext writes characters on the picture, imageline draws interference lines, and imagecolorallocate adds circles of different sizes to prevent machine recognition.

Set the information of the http header, and set it to send and display the jpeg format in the image. Use imagejpeg to directly generate the display image.

Finally, by combining the two pages, the verification of the verification code in the website page and the generation of random character pictures can be simply realized.

Popular articles
latest article