Phpci framework verification code instance Analysis _ PHP-php Tutorial

Source: Internet
Author: User
This article provides a detailed analysis of the ci framework verification code examples. For more information, see the CI framework.

Php code:
The code is as follows:
Class Captcha_code
{
Var $ width = '60 ';
Var $ num = '4 ';
Var $ height = '20 ';
Var $ name = 'randcode ';
Public function _ construct ($ conf = "")
{
If ($ conf! = "")
{
Foreach ($ conf as $ key => $ value)
{
$ This-> $ key = $ value;
}
}
}

Function show ()
{
Header ("Content-type: image/gif ");
/*
* Initialization
*/
$ Border = 0; // whether the border is required; 1: 0: not required
$ How = $ this-> num; // Number of digits of the verification code
$ W = $ this-> width; // image width
$ H = $ this-> height; // Image height
$ Fontsize = 5; // font size
$ Alpha = "abcdefghijkmnopqrstuvwxyz"; // verification code content 1: letter
$ Number = "023456789"; // verification code content 2: number
$ Randcode = ""; // verification code string initialization
Srand (double) microtime () * 1000000); // initialize the random number seed

$ Im = ImageCreate ($ w, $ h); // Create a verification image

/*
* Draw the basic framework
*/
$ Bgcolor = ImageColorAllocate ($ im, 255,255,255); // you can specify the background color.
ImageFill ($ im, 0, 0, $ bgcolor); // fill the background color
If ($ border)
{
$ Black = ImageColorAllocate ($ im, 0, 0, 0); // you can specify the border color.
ImageRectangle ($ im, 0, 0, $ W-1, $ H-1, $ black); // draw a border
}

/*
* Generate random characters by bit
*/
For ($ I = 0; $ I <$ how; $ I ++)
{
$ Alpha_or_number = mt_rand (0, 1); // letters or numbers
$ Str = $ alpha_or_number? $ Alpha: $ number;
$ Which = mt_rand (0, strlen ($ str)-1); // specify the character
$ Code = substr ($ str, $ which, 1); // Obtain the character
$ J =! $ I? 4: $ j + 15; // specifies the position of the painted character.
$ Color3 = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100); // character color
ImageChar ($ im, $ fontsize, $ j, 3, $ code, $ color3); // draw characters
$ Randcode. = $ code; // add a verification code string to a bit by bit
}

/*
* Add interference
*/
For ($ I = 0; $ I <5; $ I ++) // draws the background interference line
{
$ Color1 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference line color
ImageArc ($ im, mt_rand (-5, $ w), mt_rand (-5, $ h), mt_rand (20,300), mt_rand (20,200), 55, 44, $ color1); // interference line
}
For ($ I = 0; $ I <$ how * 15; $ I ++) // draws background interference points
{
$ Color2 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference point color
ImageSetPixel ($ im, mt_rand (0, $ w), mt_rand (0, $ h), $ color2); // interference point
}

// Write the verification code string to the session

// $ This-> session-> set_userdata (array ($ this-> name => $ randcode ));

$ _ SESSION [$ this-> name] = $ randcode;
/* Drawing ends */
Imagegif ($ im );
ImageDestroy ($ im );
/* Drawing ends */
}
}
?>

Call php code:
The code is as follows:
Function verify_image (){
$ Conf ['name'] = 'verify _ code'; // used as the configuration parameter
$ This-> load-> library ('Lib _ captcha ', $ conf );
$ This-> lib_captcha-> show ();
$ Yzm_session = $ this-> session-> userdata ('verify _ code ');
Echo $ yzm_session;
}

Html code:
The code is as follows:

Verification code:




Change one

Incorrect verification code




Js code:
The code is as follows:

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.