PHP uses the GD library to generate a verification code

Source: Internet
Author: User

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 ';
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.