In CI, the verification class can be done in this way. First, the practices in the manual are given.
Attach auxiliary functions
Use the following code to load the verification code auxiliary function:
$ This-> load-> helper ('captcha ');
The available functions are as follows:
Captcha_create ($ data)
Create a verification code image based on a series of parameters you specify. The returned value is an array containing the image data.
[Array]
(
'Image' => Image Tag
'Time' => timestamp (MS)
'Word' => CAPTCHA word
)
"Image" indicates the Image Tag actually exists:
The "time" here is a millisecond-level timestamp, used as the image file name (excluding the extension). It is like this: 1139612155.3422
"Word" is a verification code. If it is not provided, it will be a random string.
Auxiliary functions using the verification code:
After loading, you can generate a verification code like this:
$ Vals = array (
'Word' => 'random word ',
'Img _ path' => './CAPTCHA /',
'Img _ url' => 'HTTP: // example.com/captcha /',
'Font _ path' => './path/to/fonts/texb. ttf ',
'Img _ width' => '000000 ',
'Img _ height' => 30,
'Expiration' => 7200
);
$ Cap = create_captcha ($ Vals );
Echo $ CAP ['image'];
The auxiliary function of the verification code must be in the GD library.
Only img_path and img_url parameters are required.
If "word" is not provided, an ASCII string is automatically generated. You can also use your own dictionary to randomly select from it.
If the path of the True Type font is not provided, the built-in GD font will be used.
The "CAPTCHA" directory must be writable (666, or 777)
"Expiration" (seconds) specifies the time-out deletion time of the Verification Code image. The default value is 2 hours.
Database cooperation
To use verification when submitting a form, you need to save the results generated by create_captcha () to the database. In this way, when a user submits a form, you can verify whether the database has the verification code or whether the verification code has expired.
This is an example of a data table:
Create Table CAPTCHA (
Captcha_id bigint (13) unsigned not null auto_increment,
Captcha_time int (10) unsigned not null,
Ip_address varchar (16) default '0' not null,
Word varchar (20) not null,
Primary Key 'captcha _ id' ('captcha _ id '),
Key 'word' ('word ')
);
This is an example of using a database. A page with a verification code is displayed as follows:
$ This-> load-> helper ('captcha ');
$ Vals = array (
'Img _ path' => './CAPTCHA /',
'Img _ url' => 'HTTP: // example.com/captcha /'
);
$ Cap = create_captcha ($ Vals );
$ DATA = array (
'Captcha _ time' => $ CAP ['time'],
'IP _ address' => $ this-> input-> ip_address (),
'Word' => $ CAP ['word']
);
$ Query = $ this-> DB-> insert_string ('captcha ', $ data );
$ This-> DB-> query ($ query );
Echo 'submit the following verification code :';
Echo $ CAP ['image'];
Echo '<input type = "text" name = "CAPTCHA" value = ""/> ';
After the page is submitted, perform the following operations:
// Delete the old verification code first
$ Expiration = Time ()-7200; // 2 hours
$ This-> DB-> query ("delete from CAPTCHA where captcha_time <". $ expiration );
// Check whether the verification code exists:
$ SQL = "select count (*) as count from CAPTCHA where WORD =? And ip_address =? And date>? ";
$ Binds = array ($ _ post ['captcha '], $ this-> input-> ip_address (), $ expiration );
$ Query = $ this-> DB-> query ($ SQL, $ binds );
$ ROW = $ query-> row ();
If ($ row-> COUNT = 0)
{
Echo "you must submit the verification code displayed on the image ";
}
The first method is to verify whether the verification code has expired and is strict during submission. The second method is to define a simple one, such as captha. php. The Code is as follows:
? PHP
Session_start ();
// Logon Verification Code
Function getverify ($ length)
{
$ Strings = array ('3', '4', '5', '6', '7', 'A', 'B', 'C ', 'D', 'E', 'F', 'h', 'I', 'J', 'k', 'M', 'n', 'P ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y ');
$ Chrnum = "";
$ COUNT = count ($ strings );
For ($ I = 1; $ I <= $ length; $ I ++) {// generate a string using random characters in a loop
$ Chrnum. = $ strings [rand (0, $ count-1)];
}
Return $ chrnum;
}
Function Code (){
$ Fontsize = 20; // defines the font size.
$ Length = 5; // define the string length
$ Strnum = getverify ($ length); // obtain a random string
$ _ Session ['verify '] = $ strnum; // pay the value to the session
$ Width = 90; // defines the image width.
$ Height = 30; // define the Image Height
$ Im = imagecreate ($ width, $ height); // generates an image with the specified width and height.
$ Backgroundcolor = imagecolorallocate ($ im, 255,255,255); // generates the background color.
$ Framecolor = imagecolorallocate ($ im, 150,150,150); // generate the border color
$ Font = './system/fonts/Arial. ttf'; // extract the font file and start writing
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Chary = ($ height + 9)/2 + rand (-); // define the Y coordinate of the character
$ Charx = $ I * 15 + 8; // defines the X coordinate of the character
// Generate Character Color
$ Text_color = imagecolorallocate ($ im, mt_rand (50,200), mt_rand (50,128), mt_rand (50,200 ));
$ Angle = rand (-20, 20); // generate the character Angle
// Write characters
Imagettftext ($ im, $ fontsize, $ angle, $ charx, $ chary, $ text_color, $ font, $ strnum [$ I]);
}
For ($ I = 0; $ I <= 5; $ I ++) {// draws the background line cyclically.
$ Linecolor = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
$ LinEx = mt_rand (1, $ width-1 );
$ Liney = mt_rand (1, $ height-1 );
Imageline ($ im, $ LinEx, $ Liney, $ LinEx + mt_rand (0, 4)-2, $ Liney + mt_rand (0, 4)-2, $ linecolor );
}
For ($ I = 0; $ I <= 32; $ I ++) {// draws the background point cyclically to generate the pitting effect.
$ Pointcolor = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagesetpixel ($ im, mt_rand (1, $ width-1), mt_rand (1, $ height-1), $ pointcolor );
}
Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ framecolor); // draw a border
// Ob_clean ();
// Header ('content-type: image/PNG ');
Imagepng ($ IM );
Imagedestroy ($ IM );
}
?>
Then put the file in helper, and the call can be like this:
$ This-> load-> helper ('captcha ');
Code ();
The third method, for more details, for foreigners, please connect:
Http://www.okadadesign.no/blog/codeigniter/how-to-use-codeigniter-captcha-plug-in/