A simple PHP verification code implementation code

Source: Internet
Author: User
To improve website security, verification codes are essential for login. A simple and exquisite PHP Verification Code came into being! This verification code is simple and elegant, and the source code is simple. You can customize and modify the style. It is a good verification code.

To improve website security, verification codes are essential for login. A simple and exquisite PHP Verification Code came into being! This verification code is simple and elegant, and the source code is simple. You can customize and modify the style. It is a good verification code.

Implementation Code:

// Save the verification code to the session for global use
Session_start ();
$ Nums = "";
For ($ I = 0; $ I <4; $ I ++ ){
// Generate a random number and convert it to hexadecimal notation
$ Nums. = dechex (mt_rand (0, 15 ));
}
// Write the verification code to the session
$ _ SESSION ['code'] = $ nums;

// Set the length and width of the Verification Code
$ _ Width = 60;
$ _ Height = 20;
// Create an image
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );
// Create a white
$ _ White = imagecolorallocate ($ _ img, 220,250,250 );
// Fill in the background
Imagefill ($ _ img, 0, 0, $ _ white );

// Draw 6 lines randomly
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imageline ($ _ img, mt_rand (0, $ _ width), mt_rand (0, $ _ width), mt_rand (0, $ _ width), mt_rand (0, $ _ width), $ _ rnd_color );
}

// Randomly plot the snowflake
For ($ I = 0; $ I <60; $ I ++ ){
Imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ height), "*", imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 )));
}

// Output the verification code
For ($ I = 0; $ I Imagestring ($ _ img, mt_rand (6, 10), $ I * $ _ width/4 + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], imagecolorallocate ($ _ img, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 )));
}

// Output and destroy
Header ("Content-Type: image/png ");
Imagepng ($ _ img );
Imagedestroy ($ _ img );
?>

,

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.