A simple PHP CAPTCHA implementation Code _php tutorial

Source: Internet
Author: User
Implementation code:

Save the Verification code to the session for global use
Session_Start ();
$nums = "";
for ($i =0; $i <4; $i + +) {
Generate random numbers and convert to hexadecimal
$nums. =dechex (Mt_rand (0,15));
}
Write verification code to session
$_session[' code ']= $nums;

Set the verification code length and width
$_width = 60;
$_height = 20;
Create a picture
$_img = Imagecreatetruecolor ($_width,$_height);
Create a White
$_white = Imagecolorallocate ($_img,220,250,250);
Fill background
Imagefill ($_img,0,0,$_white);

Randomly draw 6 lines
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);
}

To draw a snowflake randomly
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 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);
?>

http://www.bkjia.com/phpjc/768133.html www.bkjia.com True http://www.bkjia.com/phpjc/768133.html techarticle implementation code:? PHP// Save the Verification code to the session for global use Session_Start (); $nums = ""; for ($i =0; $i 4; $i + +) {//generates a random number and converts it to a hexadecimal $nums. =dechex (Mt ...


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