Note: The following code requires opening the gd library of the php tutorial, modifying the configuration of the php.in file, and canceling the semicolon before the commented out line: extension=php_gd2.dll.
$width = 165;
$height = 120;
$image = imagecreatetruecolor($width,$height);
$bg_color = imagecolorallocate($image,255,255,255);
$tm = imagecolorallocate($image,255,0,0);
Imagefilledrectangle($image,0,0,$width,$height,$bg_color);
Imagefill($image,0,0,$bg_color);
/*
//$text = random_text(5);
$text = "ffff";
$font = 8;
$struft=iconv("gbk","utf-8",$text);
$x = imagesx($image);
$y = imagesy($image);
$fg_color = imagecolorallocate($image,233,14,91);
Imagestring($image,$font,$x,$y,$struft,$fg_color);
$_session['captcha'] = $text;
*/
header("content-type:image/png");
Imagepng($image);
imagedestroy($image);
Example 2
validate.php
Adopts session recognition, slightly improved the PHP verification code currently circulating on the Internet, added miscellaneous dots, randomly displayed numbers and colors, and controlled 4-digit display;
session_start();
//Generate verification code image
header("content-type: image/png");
$im = imagecreate(44,18);
$back = imagecolorallocate($im, 245,245,245);
imagefill($im,0,0,$back); //Backgroundsrand((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));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}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);
}
imagepng($im);
imagedestroy($im);$_session['vcode'] = $vcodes;
?>
See the complete example below
@header("content-type:text/html; charset=utf-8");
//Open session
session_start();
?>
php verification code example
Verification code:
php determines whether the verification code entered by the user is consistent with the one generated by the system
//Open session
session_start();
//Get the verification code entered by the user and convert it to uppercase
$imgid_req = $_request['imgid'];
$imgid_req = strtoupper($imgid_req);
//Verify whether the string is registered with the session variable
if (session_is_registered($imgid_req)) {
echo "Verified!";
} else {
echo "Verification error!";
}
//Close the session to clear all registered variables
session_destroy();
?>