A simple and safe PHP verification code class is attached with a call method, and a php verification code is attached with a call. A simple and safe PHP verification code class is attached with the call method, php verification code is attached with Call 1, verification code example 2, php verification code class, secoder. class. phpphp *** security verification code *** a simple and secure PHP verification code class with a call method, and a php verification code with a call
I. verification code example
II. php verification code class, secoder. class. php
<? Php/*** security verification code **: the verification code text is distorted and rotated. different fonts are used to add the interference code ** @ author
* @ Link http://labs.yulans.cn/YL_Security_Secoder * @ Link http://wiki.yulans.cn/docs/yl/security/secoder */Class YL_Security_Secoder {/*** subscripts of the verification code session ** @ var string * // public static $ seKey = 'Sid. sek ey.ylans.cn '; public static $ seKey = 'Sid'; public static $ expire = 3000; // The characters used in the verification code Expiration Time (s, 01IO is easy to confuse. we recommend that you do not use ** @ var string */public static $ codeSet = 'Your 89abcdefghjklmnpqrtuvwxy'; public static $ fontSize = 25; // The font size (px) of the verification code) public static $ useCurve = true; // whether to draw a confusion curve public static $ useNoise = true; // whether to add a miscellaneous public static $ imageH = 0; // verification code Image width public static $ imageL = 0; // verification code Image length public static $ length = 4; // verification code digits public static $ bg = array (243,251,254 ); // background protected static $ _ image = null; // verification code image instance protected static $ _ color = null; // font color of the verification code/*** output the verification code and save * the verification code saved to the session in the format of $ _ session [self :: $ seKey] = array ('code' => 'verification code value', 'Time' => 'verification code creation time'); */public static function entry () {// image width (px) self ::$ imageL | self ::$ imageL = self ::$ length * self ::$ fontSize * 1.5 + self :: $ fontSize * 1.5; // Image height (px) self: $ imageH | self ::$ imageH = self ::$ fontSize * 2; // create a self:: $ imageL x self ::$ imageH's image self ::$ _ image = imagecreate (self ::$ imageL, self ::$ imageH ); // set the background imagecolorallocate (self: $ _ image, self: $ bg [0], self: $ bg [1], self :: $ bg [2]); // random color of the verification code font self: $ _ color = imagecolorallocate (self: $ _ image, mt_rand (1,120), mt_rand (1,120 ), mt_rand (1,120); // the verification code uses a random font // $ ttf = dirname (_ FILE __). '/ttfs /'. mt_rand (1, 20 ). '. ttf'; 4 $ ttf = dirname (_ FILE __). '/ttfs/4. ttf'; if (self ::$ useNoise) {// plot the miscellaneous self ::_ writeNoise () ;}if (self ::$ useCurve) {// paint interference line self: _ writeCurve () ;}// paint verification code $ code = array (); // verification code $ codeNX = 0; // the left margin of the N character of the verification code for ($ I = 0; $ I
0) {imagesetpixel (self: $ _ image, $ px + $ I, $ py + $ I, self: $ _ color ); // the image pixels here have much better performance than imagettftext and imagestring $ I -- ;}}$ A = mt_rand (1, self ::$ imageH/2 ); // amplitude $ f = mt_rand (-self: $ imageH/4, self: $ imageH/4); // Offset of the X axis direction $ T = mt_rand (self :: $ imageH * 1.5, self: $ imageL * 2); // cycle $ w = (2 * M_PI)/$ T; $ B = $ py-$ A * sin ($ w * $ px + $ f)-self: $ imageH/2; $ px1 = $ px2; $ px2 = self:: $ imageL; for ($ px = $ px1; $ Px <= $ px2; $ px = $ px + 0.9) {if ($ w! = 0) {$ py = $ A * sin ($ w * $ px + $ f) + $ B + self: $ imageH/2; // y = Asin (ω x + phi) + B $ I = (int) (self: $ fontSize-8)/4); while ($ I> 0) {imagesetpixel (self ::$ _ image, $ px + $ I, $ py + $ I, self ::$ _ color); // here (while) the performance of looping pixel points is much better than that of imagettftext and imagestring in the font size. $ I --;}}}} /*** draw a noise * write letters or numbers of different colors to the image */protected static function _ writeNoise () {for ($ I = 0; $ I <10; $ I ++) {// Miscellaneous color $ noiseCol Or = imagecolorallocate (self: $ _ image, mt_rand (150,225), mt_rand (150,225), mt_rand (150,225); for ($ j = 0; $ j <5; $ j ++) {// draw a miscellaneous imagestring (self: $ _ image, 5, mt_rand (-10, self: $ imageL), mt_rand (-10, self:: $ imageH), self: $ codeSet [mt_rand (0, 27)], // The miscellaneous text is a random letter or number $ noiseColor );}}} /*** verify that the verification code is correct ** @ param string $ code user verification code * @ param bool user verification code is correct */public static function check ($ code ){ Isset ($ _ SESSION) | session_start (); // the verification code cannot be empty. if (empty ($ code) | empty ($ _ SESSION [self :: $ seKey]) {// echo $ _ SESSION [self: $ seKey] ['code']. '1'; return false;} // session expiration if (time ()-$ _ SESSION [self: $ seKey] ['Time']> self: $ expire) {unset ($ _ SESSION [self: $ seKey]); // echo $ _ SESSION [self: $ seKey] ['code']. '2'; return false; // return 0;} // if ($ code = $ _ SESSION [self: $ seKey] ['code']) {if (strto Upper ($ code) == _ SESSION [self: $ seKey] ['code']) {// case-insensitive comparison // echo $ _ SESSION [self :: $ seKey] ['code']. '3'; return true;} // echo $ _ SESSION [self: $ seKey] ['code']. '4'; return false ;}/// useage/* YL_Security_Secoder: $ useNoise = false; // change trueYL_Security_Secoder: $ useCurve = true to ensure higher security; YL_Security_Secoder :: entry (); * // verify the verification code if (! YL_Security_Secoder: check (@ $ _ POST ['secode']) {print 'error secret ';}*/
III. call methods
1. display the verification code page code. php
<? Php session_start (); require 'secoder. class. php'; // first include the class, and modify the actual path according to the actual situation. $ Vcode = new YL_Security_Secoder (); // instantiate an object $ vcode-> entry ();?>
2. check whether the verification code is correct
<? Php session_start (); require 'secoder. class. php'; // first include the class, and modify the actual path according to the actual situation. $ Vcode = new YL_Security_Secoder (); // instantiate an object // $ vcode-> entry (); $ code = $ _ GET ['code']; echo $ vcode-> check ($ code); // $ _ SESSION ['code'] = $ vc-> getCode (); // the verification code is saved to the SESSION?>
3. verification code input box call page
Click the image to obtain the verification code again.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.
Refer to the attached call method, php verification code attached Call 1, verification code example 2, php verification code class, secoder. class. php/*** security verification code *** security verification...