A simple and safe PHP Verification Code class with a call method, and a php verification code with a call

Source: Internet
Author: User
Tags asin

A simple and safe 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 streamline Meng Chun <cmpan () qq.com> * @ 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; // characters used in the verification code expiration time (s)/***, 01 IO 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; // The 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 picture 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 <self: $ length; $ I ++) {$ code [$ I] = self: $ codeSet [mt_rand (0, 27)]; $ codeNX + = mt_rand (self ::$ fontSize * 1.2, self :: $ fontSize * 1.6); // write a verification code character imagettftext (self ::$ _ image, self ::$ fontSize, mt_rand (-40, 70 ), $ CodeNX, self ::$ fontSize * 1.5, self ::$ _ color, $ ttf, $ code [$ I]);} // Save the verification code isset ($ _ SESSION) | session_start (); $ _ SESSION [self: $ seKey] ['code'] = join ('', $ code ); // Save the verification code to session $ _ SESSION [self: $ seKey] ['time'] = time (); // The verification code creation time header ('cache-Control: private, max-age = 0, no-store, no-cache, must-revalidate '); header ('cache-Control: post-check = 0, pre-check = 0', false); header ('pragma: no-cache' ); Header ("content-type: image/png"); // output image imagepng (self ::$ _ image); imagedestroy (self ::$ _ image );} /*** draw a random sine function curve composed of two connected functions as an interference line (you can change it to a more handsome curve function) ** in high school, I forgot about the mathematical formula. I wrote the * sine function's analytical formula: y = Asin (ω x + PHI) + B *. The effects of various constant values on function images: * A: determines the peak value (that is, the multiple of the longitudinal tensile compression) * B: indicates the position of the waveform on the Y axis or the vertical moving distance (plus or minus) * Phi: determines the relationship between the waveform and the X-axis position or the horizontal moving distance (left plus right minus) * ω: determines the cycle (minimum positive cycle T = 2 π/∣ ω ∣) **/protected static function _ writeCurve () {$ A = mt_rand (1, self: $ imageH/2); // Zhen Frame $ B = mt_rand (-self ::$ imageH/4, self ::$ imageH/4); // offset of the Y axis direction $ 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); // period $ w = (2 * M_PI)/$ T; $ px1 = 0; // the starting position of the abscissa of the curve $ px2 = mt_rand (self :: $ imageL/2, self: $ imageL * 0.667); // curve X-axis end position 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-6)/4); while ($ 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 <br> <a href = "#"> 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.