Home >Backend Development >PHP Tutorial >Generate verification code in php_PHP tutorial
Please indicate the source of original reprints on this site
session_start();
header("content-type:image/png"); Set the format of the generated image here as png
$width=40;//Width
$height=25;//Height
$color="#ffffff";//Color
$num=rand(1000,9999);//Generate a 4-digit random number
$_SESSION['ver']=$num; Verify before saving to session
$img=imagecreate($width,$height);//Use imagecreate to create images
$bg1=imagecolorallocate($img,rand(0,100),rand(0,250),rand(0,255));//Picture color
$bg2=imagecolorallocate($img,rand(0,205),rand(0,250),rand(0,245));
//imageline($img,40,20,40,40,$color);
//imagesetpixel($img,44,44,$color);
imagestring($img,7,4,7,$num,$bg2);//Increase noise
imagepng($img);//Generate image
imagedestroy($img);//Destroy
?>
This is very simple, so I won’t go into details.