How to make PHP verification code?

Source: Internet
Author: User
/**

* Make Verification code
*. Start session
Set the header
Create a canvas
Create colors
Create random numbers and place them on the canvas
*6. Placing a number of random numbers in the session
*7. Adding interference points or interference lines
*8. Output Canvas
*9. Destroying Canvas Resources
*/

1. Start session
Session_Start ();
2. Set the header to specify the MIME output type
Header (' content-type:image/png ');
3. Create a canvas
$width = 100;
$height = 30;
$im = Imagecreate ($width, $height);

4. Create colors
$bgcolor = Imagecolorallocate ($im, 255,255,255);
$textcolor = Imagecolorallocate ($im, 0,255,255);
$randcolor = Imagecolorallocate ($im, Mt_rand (0,200), Mt_rand (0,200), Mt_rand (0,200));

5. Create a random number and place it on the canvas
$verify =null;
for ($i =0; $i <4; $i + +) {
$temp = Mt_rand (0,9);
$verify. = $temp;
Imagestring ($im, 5, $i *15+15,8, $temp, Imagecolorallocate ($im, Mt_rand (0,200), Mt_rand (0,200), Mt_rand (0,200));
}


6 put the generated random number in session
$_session[' verify '] = $verify;

7. Adding interference points
for ($i =0; $i <100; $i + +) {
Imagesetpixel ($im, rand (0, $width), rand (0, $height), Imagecolorallocate ($im, Rand (100,255), Rand (100,255), Rand ( 100,255)));
}

8. Export the image
Imagepng ($im); Imagegif ()
9. Destroying an image
Imagedestroy ($im);

?>

  • 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.