国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

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

2024-05-04 23:54:54
字體:
來源:轉載
供稿:網友

是不是覺得普通的驗證碼已經沒辦法滿足,接下來介紹如何將tp現有的驗證碼改為計算型驗證碼:

首先找到: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);     // 設置背景       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; // 驗證碼創建時間    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;    }    //驗證碼是否是數字    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;    }

 調用的公共方法:

 // 檢測輸入的驗證碼是否正確,$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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 商城县| 维西| 衡水市| 旌德县| 东辽县| 大连市| 阳春市| 常山县| 广丰县| 连江县| 扎兰屯市| 营山县| 美姑县| 任丘市| 邵武市| 甘孜县| 阳江市| 营口市| 革吉县| 浦县| 新竹市| 秦安县| 陵水| 宣恩县| 静海县| 全南县| 贵州省| 宣武区| 阿城市| 苏州市| 苍梧县| 长汀县| 赣州市| 新建县| 怀安县| 织金县| 洛南县| 锦州市| 东乌珠穆沁旗| 南投市| 海安县|