Home >Backend Development >PHP Tutorial >Verification code is always wrong php verification code example code
If you want to use PHP's drawing function, you must first enable the function of this module. Just remove the comment in front of php_gd2.dll in php.ini.
Start drawing below:
Copy the code The code is as follows:
session_start();
//Generate verification code image
Header("Content-type: image/PNG");
$im = imagecreate(44,18); // Draw an image with specified width and height
$back = ImageColorAllocate($im, 245,245,245); // Define the background color
imagefill($im,0,0,$back) ; //Fill the background color into the picture just drawn
$vcodes = "";
srand((double)microtime()*1000000);
//Generate 4-digit number
for($i=0;$ i<4;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // Generate random color
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}
$_SESSION['VCODE'] = $vcodes;
for($i =0;$i<100;$i++) //Add interference pixels
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // Pixel drawing function
}
ImagePNG($im);
ImageDestroy($im);
?>
The above introduces the verification code is always wrong PHP verification code example code, including the content of the verification code is always wrong, I hope it will be helpful to friends who are interested in PHP tutorials.