SoFunction
Updated on 2025-03-06

Produce verification code based on PHP

Website registration, login or message pages all require a registration code to verify the legitimacy of the current operator, in order to prevent the website from being maliciously registered by the machine.

Generating verification code is just a few steps. First, get a random string, then create a cloth painting, write the generated string on the cloth painting. We can also draw lines and snowflakes on the cloth painting, and now post a piece of code to generate the verification code.

source code:

<?php
session_start(); //Open session//Create random code and save it in sessionfor($i=0;$i<4;$i++)
{
$_nmsg.=dechex(mt_rand(0,15));
}
//Save to session$_SESSION['code']=$_nmsg;
//Set the image length and height
$_width=75;
$_height=25;
//Create an image$_img=imagecreatetruecolor($_width,$_height);

// White background$_white=imagecolorallocate($_img,255,255,255);
//Fill on the backgroundimagefill($_img,0,0,$_white);

//Black border$_black=imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);

//Draw 5 lines immediatelyfor($i=0;$i<5;$i++)
{
$_rnd_color=imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}

//snowflakefor($i=0;$i<10;$i++)
{
$_rnd_color=imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",$_rnd_color);
}

//Output verification code
for($i=0;$i<strlen($_SESSION['code']);$i++)
{
imagestring($_img,5,10+$i*15,mt_rand(0,10),$_SESSION['code'][$i],$_blackr);
}

//Output imageheader('Content-Type:image/png');
imagepng($_img);
//Destroy the imageimagedestroy($_img);
?>

The following functions will be used in the code:

mt_rand — generate better random numbers
int mt_rand ([ int $min ], int $max ) Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() function uses the libc random number generator by default.

The mt_rand() function is used informally to replace it. This function uses a known feature in Mersenne Twister as a random number generator, which can produce random values ​​at an average speed of four times faster than rand() provided by libc.

dechex — Convert decimal to hexadecimal Returns a string containing the hexadecimal representation of the given number parameter. The maximum value that can be converted is 4294967295 in decimal, and the result is "ffffffff".

imagecreatetruecolor — Create a new true color image
resource imagecreatetruecolor ( int $x_size , int $y_size )

imagecreatetruecolor() returns an image identifier representing a black image of sizes x_size and y_size.

imagecolorallocate — Assign color to an image
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() Returns an identifier representing the color composed of the given RGB component. red, green and blue are the red, green and blue ingredients of the required colors respectively. These parameters are integers from 0 to 255 or 0x00 to 0xFF in hexadecimal. imagecolorallocate() must be called to create each color used in the image represented by image.

imagefill — area filling
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() performs area filling with color color at the coordinates x and y of the image image (the upper left corner of the image is 0, 0) (that is, the same color as the x and y points and adjacent points will be filled).

imagerectangle — Draw a rectangle
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagerectangle() Use col color to draw a rectangle in the image image, with the upper left corner coordinates being x1, y1 and the lower right corner coordinates being x2, y2. The upper left corner coordinates of the image are 0, 0.

imageline — Draw a line segment
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() Use color color to draw a line segment from coordinates x1, y1 to x2, y2 (0, 0 in the upper left corner of the image).

imagestring — draw a line of string horizontally
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
imagestring() Use col color to draw the string s to the x and y coordinates of the image represented by image (this is 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, use built-in fonts.

imagepng — output images to browser or file in PNG format
imagepng() outputs the GD image stream in PNG format to standard output (usually the browser), or outputs it to the file name if the filename is given with filename.

imagedestroy — destroy an image

imagedestroy() Frees memory associated with image.

Save the source code as a php file, how should we use it?

imagepng has outputted this php file into a png file

Just call it directly

<img src=""/>

If you want to use the verification code, remember to enable session

<?php
session_start();
echo $_SESSION['code'];
?>

I hope this article will be helpful to everyone's PHP programming.