The GD library is a powerful php library for image operations.
Add a line of reference in PHP. ini: Extension = php_gd2.dll
Restart Apache. Make a test page var_dump (gd_info (). The output data indicates that the GD database is referenced successfully.
Table ticket auth.html
<HTML>
<Head>
<Meta http-equiv = 'content-type' content = 'text/html; charset = UTF-8 '>
<Title> Verification Code </title>
</Head>
<Body>
<H1> enter the Verification Code <Form action = "check_auth.php" method = "Post">
<Input name = "auth" type = "text">
<Input type = "Submit" value = "Submit">
</Form>
</Body>
</Html>
Generate Verification Code Auth. php
<? PHP
Session_start ();
Header ("Content-Type: image/PNG ");
$ Img_width = 100;
$ Img_height = 20;
Srand (microtime () * 100000 );
For ($ I = 0; $ I <4; $ I ++)
{
$ New_number. = dechex (RAND (0, 15 ));
}
$ _ Session [check_auth] = $ new_number;
$ New_number = imagecreate ($ img_width, $ img_height); // create an image
Imagecolorallocate ($ new_number, 255,255,255); // set the background color to white.
For ($ I = 0; $ I <strlen ($ _ session [check_auth]); $ I ++)
{
$ Font = mt_rand (3, 5 );
$ X = mt_rand (1, 8) + $ img_width * $ I/4;
$ Y = mt_rand (1, $ img_height/4 );
$ Color = imagecolorallocate ($ new_number, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200); // set the Character Color
Imagestring ($ new_number, $ font, $ X, $ y, $ _ session [check_auth] [$ I], $ color); // output character
}
Imagepng ($ new_number );
Imagedestroy ($ new_number );
?>
Submit page check_auth.php
<? PHP
Session_start ();
$ Auth = $ _ post ['auth'];
If (empty ($ auth ))
{
Echo 'error: The Verification Code cannot be blank ';
Die;
}
If ($ auth = $ _ session ['check _ auth'])
{
Echo 'true ';
}
Else
{
Echo 'error: Incorrect verification code input ';
}
?>