CodeIgniter framework Verification Code class library file and usage example, codeigniter class library
This document describes the CodeIgniter framework verification code library file and usage. We will share this with you for your reference. The details are as follows:
After four or five hours, the ci verification code library was successfully integrated.
See the source code below:
Create the Authcode. php file in application/libraries. The Code is as follows:
<? Phpclass Authcode {var $ CI; var $ fontPath; // font path var $ image; var $ charLen = 4; // Generate several verification codes var $ arrChr = array (); // Verification Code character var $ width = 83; // picture width var $ height = 24; // picture height var $ bgcolor = "# ffffff "; // background color var $ showNoisePix = true; // generate the miscellaneous var $ noiseNumPix = 80; // generate the number of miscellaneous var $ showNoiseLine = true; // generate the miscellaneous var $ noiseNumLine = 2; // generate the number of miscellaneous lines var $ showBorder = true; // border. When the miscellaneous points and lines work together, the border is vulnerable to interference var $ borderColor = "#0 00000 "; function Authcode () {$ this-> CI = & get_instance (); $ this-> fontPath = realpath (dirname (_ FILE __). '/fonts/'); // font file // $ this-> arrChr = array_merge (range (1, 9), range ('A', 'z ')); // alphanumeric Verification Code // $ this-> arrChr = range ('A', 'z'); // alphanumeric Verification Code $ this-> arrChr = range (0, 9); // pure digital verification code}/*** display Verification Code **/function show () {$ this-> image = imageCreate ($ this-> width, $ this-> height); $ this-> back = $ this-> getColor ($ This-> bgcolor); imageFilledRectangle ($ this-> image, 0, 0, $ this-> width, $ this-> height, $ this-> back ); $ size = $ this-> width/$ this-> charLen-4; if ($ size> $ this-> height) {$ size = $ this-> height ;} $ left = ($ this-> width-$ this-> charLen * ($ size + $ size/10)/$ size + 5; $ code = ''; for ($ I = 0; $ I <$ this-> charLen; $ I ++) {$ randKey = rand (0, count ($ this-> arrChr) -1); $ randText = $ this-> arrChr [$ RandKey]; $ code. = $ randText; $ textColor = imageColorAllocate ($ this-> image, rand (0,100), rand (0,100), rand (0,100); $ font = $ this-> fontPath. '/'. rand (1, 5 ). ". ttf "; $ randsize = rand ($ size-$ size/10, $ size + $ size/10 ); $ location = $ left + ($ I * $ size + $ size/10); @ imagettftext ($ this-> image, $ randsize, rand (-18, 18 ), $ location, rand ($ size-$ size/10, $ size + $ size/10) + 2, $ text Color, $ font, $ randText);} if ($ this-> showNoisePix = true) {$ this-> setNoisePix ();} if ($ this-> showNoiseLine = true) {$ this-> setNoiseLine ();} if ($ this-> showBorder = true) {$ this-> borderColor = $ this-> getColor ($ this-> borderColor); imageRectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ this-> borderColor);} $ this-> CI-> session-> set_userdata ('auth _ Code', $ code ); ob_clean (); Header ("Content-type: image/jpeg"); imagejpeg ($ this-> image); imagedestroy ($ this-> image );} /*** JS call for displaying the verification code **/function showscript () {// display the verification code echo "var img_src = '/imgauthcode/show /? '; \ N "; echo" document. writeln (' ');";} /*** check whether the verification code is correct ** @ param string $ auth_code * @ return bool */function check ($ auth_code = null) {return ($ this-> CI-> session-> userdata ('auth _ Code') & $ auth_code )? ($ This-> CI-> session-> userdata ('auth _ Code') ===$ auth_code): false;} function getColor ($ color) {$ color = eregi_replace ("^ #", "", $ color); $ r = $ color [0]. $ color [1]; $ r = hexdec ($ r); $ B = $ color [2]. $ color [3]; $ B = hexdec ($ B); $ g = $ color [4]. $ color [5]; $ g = hexdec ($ g); $ color = imagecolorallocate ($ this-> image, $ r, $ B, $ g ); return $ color;} function setNoisePix () {for ($ I = 0; $ I <$ this-> noiseNumPix; $ I ++) {$ randColor = imageColorAllocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255); imageSetPixel ($ this-> image, rand (0, $ this-> width), rand (0, $ this-> height), $ randColor) ;}} function setNoiseLine () {for ($ I = 0; $ I <$ this-> noiseNumLine; $ I ++) {$ randColor = imageColorAllocate ($ this-> image, rand (0,255), rand (0,255 ), rand (0,255); imageline ($ this-> image, rand (1, $ this-> width), rand (1, $ this-> height), rand (1, $ this-> width), rand (1, $ this-> height), $ randColor );}}}
Authcode. php code ended
In Controller, there is an admin class, which has two methods:
Class Admin extends CI_Controller {function _ construct () {parent ::__ construct (); $ this-> load-> library ('authcode');} function captcha () {if ($ _ POST) {if ($ this-> authcode-> check ($ this-> input-> post ('gd _ pic '))) {echo "right";} The else {echo 'verification code is incorrect. Enter ';}} else {$ this-> load-> view ('Demo ');}} function show_captcha () {// this method is used to display the verification code image. Call $ this-> authcode-> show ();} to return the img src in the view ();}}
The following is a demo. php file created in the view. The Code is as follows:
<? Php echo form_open ('C = admin & m = captcha ');?> <Input type = "text" name = "gd_pic"/> "> <Br> <input type =" submit "name =" submit "value =" verify "/> <? Php echo form_close ();?>
OK. Everything is finished and it is running properly.