A simple and secure PHP code class with Call method, PHP verification code attached to call _php tutorial

Source: Internet
Author: User
Tags asin image png set background

A simple and secure PHP code class with Call method, PHP verification code attached to the call


Example of a verification code

Second, PHP Verification code class, secoder.class.php

<?php/** * Security Code * * Security Code to: Verify code Text Twist, rotate, use different fonts, add interference code * * @author Running water Mengchun
 
  
 * @link Http://labs.yulans.cn/YL_Security_Secoder * @link http://wiki.yulans.cn/docs/yl/security/secoder */class Yl_ The subscript of the session of the Security_secoder {/** * Verification code * * @var String *///public static $seKey = ' Sid.sek ey.ylans.cn '; public static $seKey = ' Sid ';  public static $expire = 3000; Verification code Expiration Time (s)/** * The characters used in the verification code, 01IO is easy to confuse, it is recommended not to * * @var String */public static $codeSet = ' 346789ABCDEFGHJKLMNPQRTUVWXY ';  public static $fontSize = 25; Captcha font size (px) public static $useCurve = true; Whether to draw the confusion curve public static $useNoise = true;  Whether to add a miscellaneous public static $imageH = 0;  Captcha picture wide public static $imageL = 0;  Captcha picture Long public static $length = 4; Verify the number of code bits public static $BG = Array (243, 251, 254);  Background protected static $_image = NULL;  Authenticode picture instance protected static $_color = NULL;  CAPTCHA Font Color/** * Output Verification code and save the value of the verification code in the session * verification code saved to session format: $_session[self:: $seKey] = array (' Code ' = ' captcha value ', ' time ' = ' Verification Code creation time '); */public static function entry () {//Picture width (px) Self:: $imageL | | self:: $imageL = Self:: $length * Self:: $fontSize * 1.5 + self:: $fontSize *1.5; Picture height (px) Self:: $imageH | | Self:: $imageH = self:: $fontSize * 2;  Create a self:: $imageL x Self:: $imageH image self::$_image = Imagecreate (self:: $imageL, Self:: $imageH);  Set Background imagecolorallocate (Self::$_image, Self:: $BG [0], Self:: $BG [1], Self:: $BG [2]); CAPTCHA font random Color Self::$_color = imagecolorallocate (Self::$_image, Mt_rand (1,120), Mt_rand (1,120), Mt_rand (1,120)); The captcha uses a random font//$ttf = DirName (__file__). '/ttfs/'. Mt_rand (1, 20). '. TTF '; 4 $ttf = dirname (__file__).   '/ttfs/4.ttf ';  if (self:: $useNoise) {//Plot dot self::_writenoise ();}  if (self:: $useCurve) {//Draw interference line Self::_writecurve ();} Plot verification Code $code = Array (); Verification code $codeNX = 0; The left margin of the nth character of the captcha for ($i = 0; $i
  
   
 0) {Imagesetpixel (self::$_image, $px + $i, $py + $i, self::$_color);//This is a lot better than Imagettftext and imagestring. $i ;     }}} $A = Mt_rand (1, Self:: $imageH/2); Amplitude $f = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); X-axis direction Offset $T = Mt_rand (self:: $imageH *1.5, Self:: $imageL);  Cycle $w = (M_PI)/$T; $b = $py-$A * sin ($w * $px + $f)-Self:: $imageH/2; $PX 1 = $px 2; $px 2 = self:: $imageL; for ($px = $px 1; $px <= $px 2, $px = $px + 0.9) {if ($w!=0) {$py = $A * Sin ($w * $px + $f) + $b + self:: $imageH/2;//y = Asin (ω x+φ) + b $i = (int) ((self:: $fontSize-8)/4); while ($i > 0) {imagesetpixel (self::$_image, $px + $i, $py + $i, self::$_color);//here (while) the cycle of the image point is more than Imagettftext and IMA Gestring with a font size (without this while loop) the performance is much better $i-; }}}}/** * Draw the dots * to write letters or numbers of different colors on the picture * * protected static function _writenoise () {for ($i = 0; $i < $i + +) {//Matte color $ Noisecolor = Imagecolorallocate (Self::$_image, Mt_rand (150,225), Mt_rand (150,225), Mt_rand (150,225 )     ); for ($j = 0; $J < 5; $j + +) {//Plot point imagestring (Self::$_image, 5, Mt_rand ( -10, Self:: $imageL), Mt_rand ( -10, Self:: $imageH), self :: $codeSet [Mt_rand (0, 27)],//Miscellaneous text for random letters or numbers $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) {Isse T ($_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 expires 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 (Strtoupper ($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; To be more secure, change to Trueyl_security_secoder:: $useCurve = tRue Yl_security_secoder::entry (); *//*//Verification code if (! Yl_security_secoder::check (@$_post[' Secode ')) {print ' Error secode ';} */
  
 

Third, call method

1. Display Verification code page code.php

<?php  session_start (); require ' secoder.class.php ';//first include the class, the actual path is modified according to the actual situation.  $vcode = new Yl_security_secoder ();  Instantiate an Object  

2. Check that the verification code is correct

<?php  session_start (); require ' secoder.class.php ';//first include the class, the actual path is modified according to the actual situation.  $vcode = new Yl_security_secoder ();  Instantiate an object  //$vcode->entry ();  $code = $_get[' code ');  echo $vcode->check ($code);   

3. Verification Code Input Box call page

Click the picture to retrieve the verification code again

The above is the whole content of this article, I hope that everyone's learning to help, but also hope that we support a lot of help the home.

http://www.bkjia.com/PHPjc/1138985.html www.bkjia.com true http://www.bkjia.com/PHPjc/1138985.html techarticle a simple and secure PHP code class with Call method, PHP verification code attached to call one, code example two, PHP verification code class, secoder.class.php php/** * Security Code * * Security verification ...

  • Related Article

    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.