PHP verification code generation example

Source: Internet
Author: User

Follow these steps to create a verification code using the php gd Library Extension class:
Enable extension = php_gd2.dll extension in the php. ini configuration file (open the php. ini configuration file, remove the semicolon at the beginning of the line, and restart Apache );
The following code generates a verification code using PHP:

The code is as follows: Copy code

<? Php
For ($ I = 0; $ I <5; $ I ++ ){
$ Suijishu. = dechex (rand (); // use a loop to generate a five-digit hexadecimal format
}
Setcookie ("useryzm", $ suijishu); // Create a cookie named useryzm with the value of $ suijishu
$ Img = imagecreatetruecolor (100, 30); // create an image verification code with a width of and a height of 30.
$ Bg = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255); // defines the background color of the verification code.
$ SC = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255); // defines the font color of the verification code.
Imagestring ($ img, 5, rand (), rand (), $ suijishu, $ SC); // defines the font position (the coordinate origin in the upper left corner)
For ($ k = 0; $ k <10; $ k ++ ){
Imageline ($ img, rand (1,100), rand (1,100), rand (1,100), rand (1,100), $ SC); // draw a straight line on the verification code (disturbing)
Imagesetpixel ($ img, rand (1,100), rand (), $ SC); // draw points on the verification code (disturbing)
}
Ob_clean (); // clear the output buffer (This sentence is very important)
Header ("Content-type: image/jpeg"); // defines the format of the output image (image/can be followed by jpeg, png, gif, and other image formats)
Imagejpeg ($ img); // output image
ImageDestroy ($ img); // release the memory and complete the creation.
?>

The following code calls the verification code in HTML:

The code is as follows: Copy code

<Html>
...
<Script language = "javascript">
Function changeyzm (){
Var img = document. getElementById ("yzmimg"). src = "gdyzm. php? Id = "+ Math. random () * 10;
Return;
}
</Script>
...

...
</Html>

If the error "image" XXXX "cannot be displayed due to its own mistakes" appears, it is likely that ob_clean () is missing, or there is a problem with the physical encoding of the file (for details about how to modify the physical encoding, refer to PHP to create a utf8 file)
If ob_clean (); is not added, it is okay to test locally and the physical code is ANSI, when the physical code is UTF-8, an error is reported (image "XXXX" cannot be displayed because of its own error)

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.