既簡(jiǎn)單又安全的PHP驗(yàn)證碼 附調(diào)用方法

字號(hào):


    這篇文章主要為大家分享了一段實(shí)用的PHP驗(yàn)證碼類(lèi),既簡(jiǎn)單又安全的PHP驗(yàn)證碼代碼,感興趣的小伙伴們可以參考一下
    一、驗(yàn)證碼示例
    名單
    二、php驗(yàn)證碼類(lèi),secoder.class.php
    <?php 
    /** 
     * 安全驗(yàn)證碼 
     * 
     * 安全的驗(yàn)證碼要:驗(yàn)證碼文字扭曲、旋轉(zhuǎn),使用不同字體,添加干擾碼 
     * 
     * @author 流水孟春 <cmpan(at)qq.com> 
     * @link http://labs.yulans.cn/YL_Security_Secoder 
     * @link http://wiki.yulans.cn/docs/yl/security/secoder 
     */
    class YL_Security_Secoder { 
      /** 
       * 驗(yàn)證碼的session的下標(biāo) 
       * 
       * @var string 
       */
      //public static $seKey = 'sid.sek ey.ylans.cn'; 
      public static $seKey = 'sid'; 
      public static $expire = 3000;   // 驗(yàn)證碼過(guò)期時(shí)間(s) 
      /** 
       * 驗(yàn)證碼中使用的字符,01IO容易混淆,建議不用 
       * 
       * @var string 
       */
      public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY'; 
      public static $fontSize = 25;   // 驗(yàn)證碼字體大小(px) 
      public static $useCurve = true;  // 是否畫(huà)混淆曲線(xiàn) 
      public static $useNoise = true;  // 是否添加雜點(diǎn)  
      public static $imageH = 0;    // 驗(yàn)證碼圖片寬 
      public static $imageL = 0;    // 驗(yàn)證碼圖片長(zhǎng) 
      public static $length = 4;    // 驗(yàn)證碼位數(shù) 
      public static $bg = array(243, 251, 254); // 背景 
      protected static $_image = null;   // 驗(yàn)證碼圖片實(shí)例 
      protected static $_color = null;   // 驗(yàn)證碼字體顏色 
      /** 
       * 輸出驗(yàn)證碼并把驗(yàn)證碼的值保存的session中 
       * 驗(yàn)證碼保存到session的格式為: $_SESSION[self::$seKey] = array('code' => '驗(yàn)證碼值', 'time' => '驗(yàn)證碼創(chuàng)建時(shí)間'); 
       */
      public static function entry() { 
        // 圖片寬(px) 
        self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5;  
        // 圖片高(px) 
        self::$imageH || self::$imageH = self::$fontSize * 2; 
        // 建立一幅 self::$imageL x self::$imageH 的圖像 
        self::$_image = imagecreate(self::$imageL, self::$imageH);  
        // 設(shè)置背景    
        imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]);  
        // 驗(yàn)證碼字體隨機(jī)顏色 
        self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120)); 
        // 驗(yàn)證碼使用隨機(jī)字體  
        //$ttf = dirname(__FILE__) . '/ttfs/' . mt_rand(1, 20) . '.ttf'; 4 
        $ttf = dirname(__FILE__) . '/ttfs/4.ttf';  
        if (self::$useNoise) { 
          // 繪雜點(diǎn) 
          self::_writeNoise(); 
        }  
        if (self::$useCurve) { 
          // 繪干擾線(xiàn) 
          self::_writeCurve(); 
        } 
        // 繪驗(yàn)證碼 
        $code = array(); // 驗(yàn)證碼 
        $codeNX = 0; // 驗(yàn)證碼第N個(gè)字符的左邊距 
        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); 
          // 寫(xiě)一個(gè)驗(yàn)證碼字符 
          imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize*1.5, self::$_color, $ttf, $code[$i]); 
        } 
        // 保存驗(yàn)證碼 
        isset($_SESSION) || session_start(); 
        $_SESSION[self::$seKey]['code'] = join('', $code); // 把校驗(yàn)碼保存到session 
        $_SESSION[self::$seKey]['time'] = time(); // 驗(yàn)證碼創(chuàng)建時(shí)間 
        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"); 
        // 輸出圖像 
        imagepng(self::$_image);  
        imagedestroy(self::$_image); 
      } 
      /** 
       * 畫(huà)一條由兩條連在一起構(gòu)成的隨機(jī)正弦函數(shù)曲線(xiàn)作干擾線(xiàn)(你可以改成更帥的曲線(xiàn)函數(shù)) 
       *    
       *   高中的數(shù)學(xué)公式咋都忘了涅,寫(xiě)出來(lái) 
       *   正弦型函數(shù)解析式:y=Asin(ωx+φ)+b 
       *   各常數(shù)值對(duì)函數(shù)圖像的影響: 
       *    A:決定峰值(即縱向拉伸壓縮的倍數(shù)) 
       *    b:表示波形在Y軸的位置關(guān)系或縱向移動(dòng)距離(上加下減) 
       *    φ:決定波形與X軸位置關(guān)系或橫向移動(dòng)距離(左加右減) 
       *    ω:決定周期(最小正周期T=2π/∣ω∣) 
       * 
       */
      protected static function _writeCurve() { 
        $A = mt_rand(1, self::$imageH/2);         // 振幅 
        $b = mt_rand(-self::$imageH/4, self::$imageH/4);  // Y軸方向偏移量 
        $f = mt_rand(-self::$imageH/4, self::$imageH/4);  // X軸方向偏移量 
        $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期 
        $w = (2* M_PI)/$T; 
        $px1 = 0; // 曲線(xiàn)橫坐標(biāo)起始位置 
        $px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667); // 曲線(xiàn)橫坐標(biāo)結(jié)束位置       
        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+φ) + b 
            $i = (int) ((self::$fontSize - 6)/4); 
            while ($i > 0) {  
              imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 這里畫(huà)像素點(diǎn)比imagettftext和imagestring性能要好很多          
              $i--; 
            } 
          } 
        } 
        $A = mt_rand(1, self::$imageH/2);         // 振幅     
        $f = mt_rand(-self::$imageH/4, self::$imageH/4);  // X軸方向偏移量 
        $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期 
        $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+φ) + b 
            $i = (int) ((self::$fontSize - 8)/4); 
            while ($i > 0) {      
              imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 這里(while)循環(huán)畫(huà)像素點(diǎn)比imagettftext和imagestring用字體大小一次畫(huà)出(不用這while循環(huán))性能要好很多   
              $i--; 
            } 
          } 
        } 
      } 
      /** 
       * 畫(huà)雜點(diǎn) 
       * 往圖片上寫(xiě)不同顏色的字母或數(shù)字 
       */
      protected static function _writeNoise() { 
        for($i = 0; $i < 10; $i++){ 
          //雜點(diǎn)顏色 
          $noiseColor = imagecolorallocate( 
                   self::$_image,  
                   mt_rand(150,225),  
                   mt_rand(150,225),  
                   mt_rand(150,225) 
                 ); 
          for($j = 0; $j < 5; $j++) { 
            // 繪雜點(diǎn) 
            imagestring( 
              self::$_image, 
              5,  
              mt_rand(-10, self::$imageL),  
              mt_rand(-10, self::$imageH),  
              self::$codeSet[mt_rand(0, 27)], // 雜點(diǎn)文本為隨機(jī)的字母或數(shù)字 
              $noiseColor
            ); 
          } 
        } 
      } 
      /** 
       * 驗(yàn)證驗(yàn)證碼是否正確 
       * 
       * @param string $code 用戶(hù)驗(yàn)證碼 
       * @param bool 用戶(hù)驗(yàn)證碼是否正確 
       */
      public static function check($code) { 
        isset($_SESSION) || session_start(); 
        // 驗(yàn)證碼不能為空 
        if(empty($code) || empty($_SESSION[self::$seKey])) { 
          //echo $_SESSION[self::$seKey]['code'].'1'; 
          return false; 
        } 
        // session 過(guò)期 
        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']) { //不區(qū)分大小寫(xiě)比較 
          //echo $_SESSION[self::$seKey]['code'].'3'; 
          return true;     
        } 
        //echo $_SESSION[self::$seKey]['code'].'4'; 
        return false; 
      } 
    } 
    // useage 
    /* 
    YL_Security_Secoder::$useNoise = false; // 要更安全的話(huà)改成true 
    YL_Security_Secoder::$useCurve = true; 
    YL_Security_Secoder::entry(); 
    */
    /* 
    // 驗(yàn)證驗(yàn)證碼 
    if (!YL_Security_Secoder::check(@$_POST['secode'])) { 
      print 'error secode'; 
    } 
    */
    三、調(diào)用方法
    1、顯示驗(yàn)證碼頁(yè)面code.php
    <?php  
      session_start(); 
      require 'secoder.class.php'; //先把類(lèi)包含進(jìn)來(lái),實(shí)際路徑根據(jù)實(shí)際情況進(jìn)行修改。  
      $vcode = new YL_Security_Secoder();   //實(shí)例化一個(gè)對(duì)象  
      $vcode->entry();  
    ?>  
    2、檢查驗(yàn)證碼是否正確
    <?php  
      session_start(); 
      require 'secoder.class.php'; //先把類(lèi)包含進(jìn)來(lái),實(shí)際路徑根據(jù)實(shí)際情況進(jìn)行修改。  
      $vcode = new YL_Security_Secoder();   //實(shí)例化一個(gè)對(duì)象  
      //$vcode->entry();  
      $code = $_GET['code'];  
      echo $vcode->check($code);     
      //$_SESSION['code'] = $vc->getCode();//驗(yàn)證碼保存到SESSION中 
    ?>  
    3、驗(yàn)證碼輸入框調(diào)用頁(yè)面
    <img id="messageImg" src='images/tishis2.gif' width='16' height='16'> 單擊圖片重新獲取驗(yàn)證碼<br> 
    <a href="#"><img src="code.php" onclick="javascript:this.src='code.php?tm='+Math.random();" />
    以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)PHP程序設(shè)計(jì)有所幫助。