溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Thinkphp3.2實用篇之計算型驗證碼的示例分析

發(fā)布時間:2021-07-26 14:13:54 來源:億速云 閱讀:139 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)Thinkphp3.2實用篇之計算型驗證碼的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

首先找到:ThinkPHP\Library\Think\Verify.class.php

在其中加入以下代碼:

public function entry_add($id = '') {
    $this->length='3';
    // 圖片寬(px)
    $this->imageW || $this->imageW = $this->length*$this->fontSize*1.5 + $this->length*$this->fontSize/2; 
    // 圖片高(px)
    $this->imageH || $this->imageH = $this->fontSize * 2.5;
    // 建立一幅 $this->imageW x $this->imageH 的圖像
    $this->_image = imagecreate($this->imageW, $this->imageH); 
    // 設(shè)置背景   
    imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); 

    // 驗證碼字體隨機顏色
    $this->_color = imagecolorallocate($this->_image, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150));
    // 驗證碼使用隨機字體
    $ttfPath = dirname(__FILE__) . '/Verify/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';

    if(empty($this->fontttf)){
      $dir = dir($ttfPath);
      $ttfs = array();    
      while (false !== ($file = $dir->read())) {
        if($file[0] != '.' && substr($file, -4) == '.ttf') {
          $ttfs[] = $file;
        }
      }
      $dir->close();
      $this->fontttf = $ttfs[array_rand($ttfs)];
    } 
    $this->fontttf = $ttfPath . $this->fontttf;
    
    if($this->useImgBg) {
      $this->_background();
    }
    
    if ($this->useNoise) {
      // 繪雜點
      $this->_writeNoise();
    }
    if ($this->useCurve) {
      // 繪干擾線
      $this->_writeCurve();
    }
    
    // 繪驗證碼
    $code = array(); // 驗證碼
    $symbol=array('+','-');
    $codeNX = 0; // 驗證碼第N個字符的左邊距
    $now_symbol=$symbol[rand(0,1)];
    for ($i = 0; $i<$this->length; $i++) {
      if($i==1){
        $code[$i] = $now_symbol;
        $codeNX += mt_rand($this->fontSize*1.2, $this->fontSize*1.6);
        imagettftext($this->_image, $this->fontSize,0, $codeNX, $this->fontSize*1.6, $this->_color, $ttfPath.'2.ttf', $code[$i]);
      }
      else{
        $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet)-1)];
        $codeNX += mt_rand($this->fontSize*1.2, $this->fontSize*1.6);
        imagettftext($this->_image, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize*1.6, $this->_color, $this->fontttf, $code[$i]);
      } 
    }
    
    // 保存驗證碼
    $key    =  $this->authcode($this->seKey);
    $str=implode('', $code);
    eval("\$re=$str;");
    $code    =  $this->authcode($re);
    $secode   =  array();
    $secode['verify_code'] = $code; // 把校驗碼保存到session
    $secode['verify_time'] = NOW_TIME; // 驗證碼創(chuàng)建時間
    session($key.$id, $secode);
            
    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($this->_image);
    imagedestroy($this->_image);
  }
public function check_add($code, $id = '') {
    $key = $this->authcode($this->seKey).$id;
    // 驗證碼不能為空
    $secode = session($key);
    if($code===false || empty($secode)) {
      return false;
    }
    //驗證碼是否是數(shù)字
    if(!is_numeric($code)) {
      return false;
    }
    // session 過期
    if(NOW_TIME - $secode['verify_time'] > $this->expire) {
      session($key, null);
      return false;
    }
    if($this->authcode($code) == $secode['verify_code']) {
      $this->reset && session($key, null);
      return true;
    }
    return false;
  }

生成方法:

Public function verify(){
    import('ORG.Util.Verify');
    $Verify = new Verify();
    $Verify->useNoise = true;
    $Verify->codeSet = '0123456789';
    $Verify->useCurve = false;
    $Verify->entry_add();
  }

驗證方法:

 if (!check_verify($verify,'','add')) {
      $this->error('驗證碼錯誤!');
      return;
    }

 調(diào)用的公共方法:

 // 檢測輸入的驗證碼是否正確,$code為用戶輸入的驗證碼字符串
function check_verify($code, $id = '',$type=''){
  import('ORG.Util.Verify');
  $verify = new Verify();
  if($type='add'){
    return $verify->check_add($code, $id);
  }
  else{
    return $verify->check($code, $id);
  }
}

關(guān)于“Thinkphp3.2實用篇之計算型驗證碼的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI