Example of Captcha Verification Code implemented by php, captcha Verification Code

Source: Internet
Author: User

Example of Captcha Verification Code implemented by php, captcha Verification Code

This example describes the Captcha Verification Code class implemented by php, which is widely used in php programming. Share it with you for your reference. The specific method is as follows:

The verification code file is as follows:

<? Php/** Captcha Verification Code class * Date: 2011-02-19 * Author: fdipzone */class Captcha {// class start private $ sname = ''; public function _ construct ($ sname = '') {// $ sname captcha session name $ this-> sname = $ sname = ''? 'M _ captcha ': $ sname ;} /** generate Verification Code image * @ param int $ length verification code length * @ param Array $ param digit number * @ return IMG */public function create ($ length = 4, $ param = array () {Header ("Content-type: image/PNG"); $ authnum = $ this-> random ($ length); // generate Verification Code characters. $ width = isset ($ param ['width'])? $ Param ['width']: 13; // text width $ height = isset ($ param ['height'])? $ Param ['height']: 18; // text height $ pnum = isset ($ param ['pnum'])? $ Param ['pnum']: 100; // Number of interference pixels $ lnum = isset ($ param ['lnum'])? $ Param ['lnum']: 2; // Number of interfering lines $ this-> captcha_session ($ this-> sname, $ authnum ); // write the random number to session $ pw = $ width * $ length + 10; $ ph = $ height + 6; $ im = imagecreate ($ pw, $ ph ); // imagecreate () creates a new image with a blank image of x_size and y_size. $ Black = ImageColorAllocate ($ im, 238,238,238); // set the background color $ values = array (mt_rand (0, $ pw), mt_rand (0, $ ph ), mt_rand (0, $ pw), mt_rand (0, $ ph), mt_rand (0, $ pw), mt_rand (0, $ ph), mt_rand (0, $ pw ), mt_rand (0, $ ph), mt_rand (0, $ pw), mt_rand (0, $ ph), mt_rand (0, $ pw), mt_rand (0, $ ph); imagefilledpolygon ($ im, $ values, 6, ImageColorAllocate ($ im, mt_rand (170,255), mt_rand (200,255), mt_rand (210,255 ))); // set the basemap of the interference polygon/* text * /For ($ I = 0; $ I <strlen ($ authnum); $ I ++) {$ font = ImageColorAllocate ($ im, mt_rand (0, 50 ), mt_rand (0,150), mt_rand (0,200); // set the text color $ x = $ I/$ length * $ pw + rand (1, 6 ); // set the random X coordinate $ y = rand (1, $ ph/3); // set the random Y coordinate imagestring ($ im, mt_rand (), $ x, $ y, substr ($ authnum, $ I, 1), $ font);}/* Add interference pixels */for ($ I = 0; $ I <$ pnum; $ I ++) {$ dist = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));/ /Set the noise color imagesetpixel ($ im, mt_rand (0, $ pw), mt_rand (0, $ ph), $ dist );} /* Add interference lines */for ($ I = 0; $ I <$ lnum; $ I ++) {$ dist = ImageColorAllocate ($ im, mt_rand (50,255 ), mt_rand (150,255), mt_rand (200,255); // sets the line color imageline ($ im, mt_rand (0, $ pw), mt_rand (0, $ ph ), mt_rand (0, $ pw), mt_rand (0, $ ph), $ dist);} ImagePNG ($ im ); // output the image to the browser or file ImageDestroy ($ im) in PNG format; // destroy an image}/** check the Verification Code * @ param String $ captcha Verification Code * @ param Int $ flag after successful verification 0: Do not clear session 1: Clear session * @ return boolean */public function check ($ captcha, $ flag = 1) {if (empty ($ captcha) {return false;} else {if (strtoupper ($ captcha) ==$ this-> captcha_session ($ this-> sname )) {// check the verification code if ($ flag = 1) {$ this-> captcha_session ($ this-> sname, '');} return true ;} else {return false ;}}/ * random number generation function * @ param int $ length the number of strings to be randomly generated * @ return String */private function rand Om ($ length) {$ hash = ''; $ chars = 'abcdefghijklmnpqrstuvwxyz23456789 '; $ max = strlen ($ chars)-1; for ($ I = 0; $ I <$ length; $ I ++) {$ hash. = $ chars [mt_rand (0, $ max)];} return $ hash ;} /** Verification Code session Processing Method * @ param String $ name captcha session name * @ param String $ value * @ return String */private function captcha_session ($ name, $ value = null) {if (isset ($ value) {if ($ value! = '') {$ _ SESSION [$ name] = $ value;} else {unset ($ _ SESSION [$ name]);} else {return isset ($ _ SESSION [$ name])? $ _ SESSION [$ name]: '';}}// class end?>

The demo sample program is as follows:

<? Php session_start (); require_once ('captcha. class. php '); $ obj = new Captcha ($ sname); # create a Captcha Class Object # $ sname is the session name for saving captcha, which can be left blank, leave null to'm _ captcha '$ obj-> create ($ length, $ param); # create Captcha and output the image # $ length is the length of Captcha. leave it blank, the default value is 4/* $ param = array ('width' => 13 captcha character width 'height' => 18 captcha character height 'pnum' => 100 interference points 'lnum' => 2. Number of interference lines) leave it blank */$ obj-> check ($ captcha, $ flag); # check whether the verification code entered by the user is correct, true or False # $ captcha is the verification code entered by the user. required # $ flag can be left blank. The default value is 1 #1. After the verification is successful, the captcha session is automatically cleared #0: the captcha session is not cleared after the block verification is successful. Is it used for ajax checks?>

I believe this article provides some reference value for php programming.


Which of the following is an example of generating a verification code for Zend_Captcha?-PHP framework development?

I don't understand why zend makes the default verification code so hard to recognize.
 
The verification code on the php registration page is as follows:

You need to determine whether the verification code you entered is consistent with the one you saved earlier.

If ($ _ POST ["captcha"]! = $ _ SESSION ["captcha"])
{
Echo "Incorrect 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.