Captcha verification code class instance implemented by php, captcha verification code _ PHP Tutorial

Source: Internet
Author: User
Php-implemented Captcha verification code instance and captcha verification code. Php Captcha verification code instance. This article describes the captcha verification code class implemented by php, which is widely used in php programming. I will share with you the Captcha and captcha verification codes implemented by php.

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 ";
}

Examples in this article describe the Captcha verification code class implemented by php, which is widely used in php programming. Share it with you...

Related Article

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.