Captcha Verification Code instance implemented by php

Source: Internet
Author: User
This article mainly introduces the Captcha Verification Code class implemented by php. The example shows a verification code program with a usage demonstration instance, which has great reference value. If you need it, you can refer to it.

This article mainly introduces the Captcha Verification Code class implemented by php. The example shows a verification code program with a usage demonstration instance, which has great reference value. If you need it, you can refer to it.

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 startprivate $ 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 Miscellaneous 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 a browser or file ImageDestroy ($ im) in PNG format ); // destroy an image}/** check the Verification Code * @ param String $ captcha Verification Code * @ param int $0 after flag verification is successful: No session cleared 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 random ($ leng Th) {$ 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:

<? Phpsession_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. The captcha session is automatically cleared after the verification is successful. #0: the captcha session is not cleared after the verification is successful, used for ajax check?>

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

,

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.