PHP Generate Verification Code

Source: Internet
Author: User

/**
* _code () is a CAPTCHA function
* @access Public
* @param int $_width Indicates the length of the Verification code
* @param int $_height Indicates the height of the verification code
* @param int $_rnd_code Indicates the number of digits of the verification code
* @param bool $_flag Indicates if the verification code requires a border
* @return void A verification code is generated after this function is executed
*/
function _code ($_width = 75,$_height = 25,$_rnd_code = 4,$_flag = False) {

Create Random Code
for ($i =0; $i <$_rnd_code; $i + +) {
$_nmsg. = Dechex (Mt_rand (0,15));
}

Save in session
$_session[' Code ' = $_nmsg;

Create an image
$_img = Imagecreatetruecolor ($_width,$_height);

White
$_white = Imagecolorallocate ($_img,255,255,255);

Fill
Imagefill ($_img,0,0,$_white);

if ($_flag) {
Black, border
$_black = Imagecolorallocate ($_img,0,0,0);
Imagerectangle ($_img,0,0,$_width-1,$_height-1,$_black);
}

6 lines are drawn immediately
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,$_height), Mt_rand (0,$_width), Mt_rand (0,$_height), $_rnd_color);
}

Immediately snowflakes
for ($i =0; $i <100; $i + +) {
$_rnd_color = Imagecolorallocate ($_img,mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagestring ($_img,1,mt_rand (1,$_width), Mt_rand (1,$_height), ' * ', $_rnd_color);
}

Output Verification Code
for ($i =0; $i <strlen ($_session[' code '); $i + +) {
$_rnd_color = Imagecolorallocate ($_img,mt_rand (0,100), Mt_rand (0,150), Mt_rand (0,200));
Imagestring ($_img,5, $i *$_width/$_rnd_code+mt_rand (1,10), Mt_rand (1,$_HEIGHT/2), $_session[' code '] [$i],$_rnd_ color);
}

Output image
Header (' content-type:image/png ');
Imagepng ($_IMG);

Destroyed
Imagedestroy ($_IMG);
}

PHP Generate Verification Code

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.