Simple and secure PHP code-attached invocation method _php Tips

Source: Internet
Author: User

Example of a verification code


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

<?php/** * Security Verification Code: Verify code text is distorted, rotated, using different fonts, add interference code * * @author Running water Mengchun <cmpan (at) qq.com> * @link HT Tp://labs.yulans.cn/yl_security_secoder * @link Http://wiki.yulans.cn/docs/yl/security/secoder * * Class Yl_security_ 
  Secoder {/** * Verification Code of the session of the subscript * * @var string///public static $seKey = ' Sid.sek ey.ylans.cn '; 
  public static $seKey = ' Sid ';   public static $expire = 3000; Authenticode expiration (s)/** * code used in the character, 01IO easy to confuse, recommended not * * @var string/public static $codeSet = ' 346789ABCDE 
  Fghjklmnpqrtuvwxy ';   public static $fontSize = 25;  Authenticode font size (px) public static $useCurve = true;  Whether to draw a confusing curve public static $useNoise = true;    Add pragma public static $imageH = 0;    Verify code picture wide public static $imageL = 0;    Verify code picture long public static $length = 4; Verify the number of code digits public static $BG = Array (243, 251, 254);   Background protected static $_image = NULL;   Verification code picture instance protected static $_color = NULL; 
 Verify Code Font Color  
  /** * Output Verification code and the value of the validation code saved in the session of the session * Verification code saved to the format of the session: $_session[self:: $seKey] = array (' Code ' => ' Verification Code value ', ' time 
   ' => ' Verification Code creation time '); */public static function entry () {//Picture width (px) Self:: $imageL | | self:: $imageL = self:: $length * Self:: $fontSi  
    Ze * 1.5 + self:: $fontSize *1.5; Picture High (px) Self:: $imageH | | 
    Self:: $imageH = self:: $fontSize * 2;  
    Establish 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 validation code uses a random font//$ttf = DirName (__file__). '/ttfs/'. Mt_rand (1, 20). '. TTF '; 4 $ttf = dirname (__file__).  
     
    '/ttfs/4.ttf '; 
    if (self:: $useNoise) {//Paint Miscellaneous point self::_writenoise (); } if (self:: $useCurve) {//Paint interference line Self::_writecurve (); //Draw Verification Code $code = Array (); Verification code $codeNX = 0; 
      The left margin for the nth character of the authentication 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 captcha character imagettftext (Self::$_image, Self:: $fontSize, Mt_rand ( -40), $codeNX, self:: $fontSize *1.5, Self::$_co 
    Lor, $ttf, $code [$i]); }//Save Verification Code isset ($_session) | | 
    Session_Start (); $_session[self:: $seKey] [' code '] = Join (", $code); Save the check code to session $_session[self:: $seKey [' time '] = time (); 
    Verify 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 that is composed of two lines (you can change to a more handsome curve function)* I forgot the math formula for the high school. * sinusoidal function Analytic formula: Y=asin (ωx+φ) +b * Effects of various constant values on function images: * A: Determining peak (i.e., multiple of longitudinal tensile compression) * b : Indicates the position of the waveform in the y-axis or the longitudinal distance (up and down) *φ: Determines the waveform and the position of the x axis or the transverse moving distance (left plus right minus) *ω: Decision period (minimum positive period t=2π/∣ω∣) * * * * * Protected St         atic function _writecurve () {$A = Mt_rand (1, Self:: $imageH/2);  Amplitude $b = Mt_rand (-self:: $imageH/4, Self:: $imageH/4);  Y-axis Offset $f = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); X axis Offset $T = Mt_rand (self:: $imageH *1.5, Self:: $imageL *2); 
             
    Cycle $w = (2* m_pi)/$T; $px 1 = 0; The starting position of the curve axis $px 2 = Mt_rand (self:: $imageL/2, Self:: $imageL * 0.667); The end position of the curve axis 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-6)/4); while ($i > 0) {imagesetpixel (self::$_image, $px + $i, $py + $i, self::$_color);//Here's a portrait of a vegetarian dot than imagettftext and IM AgesTring performance is much better $i--;         }} $A = Mt_rand (1, Self:: $imageH/2);  Amplitude $f = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); X axis Offset $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; 
    $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) loop portrait dot more than IM 
        Agettftext and imagestring use font size to draw (without this while loop) performance is much better $i-; 
    /** * Draw Miscellaneous points * Write different color letters or numbers to the picture * * protected static function _writenoise () { for ($i = 0; $i < $i + +) {//Miscellaneous point color $noiseColor = Imagecolorallocate ( 
               Self::$_image, Mt_rand (150,225), Mt_rand (150,225), Mt_ 
      Rand (150,225)); for ($j = 0; $j < 5 $j + +) {//Paint Miscellaneous point imagestring (Self::$_image, 5, M T_rand ( -10, Self:: $imageL), Mt_rand ( -10, Self:: $imageH), Self:: $codeSet [Mt_rand (0, 27)],//Miscellaneous point text with 
      The letter or number $noiseColor) of the machine; /** * Verify that the verification code is correct * * @param string $code user authentication code * @param BOOL user authentication code is correct/publi 
    C static function Check ($code) {isset ($_session) | | session_start (); Authenticode 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 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 True yl_security_secoder:: $useCurve = true; 
Yl_security_secoder::entry (); */*//Verify code if (! 
Yl_security_secoder::check (@$_post[' Secode ')) {print ' Error secode '; 

 } 
*/

Third, call the method

1. Display Verification code page code.php

<?php  
  session_start (); 
  Require ' secoder.class.php '; First the class is included, the actual path is modified according to the actual situation.  
  $vcode = new Yl_security_secoder ();   Instantiate an object  
  $vcode->entry ();  
? >  

2, check the verification code is correct

<?php  
  session_start (); 
  Require ' secoder.class.php '; First the class is included, 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);     
  $_session[' Code ' = $VC->getcode ()//Verify code is saved to session 
?>  

3. Verification Code Input Box call page

 click the picture to retrieve the verification code <br> 

The above is the entire content of this article, I hope that you learn PHP programming help.

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.