Thinkphp3.2實用篇之計算型驗證碼示例
是不是覺得普通的驗證碼已經(jīng)沒辦法滿足,接下來介紹如何將tp現(xiàn)有的驗證碼改為計算型驗證碼:
首先找到: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]);
// 驗證碼字體隨機(jī)顏色
$this->_color = imagecolorallocate($this->_image, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150));
// 驗證碼使用隨機(jī)字體
$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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- thinkphp5.1驗證碼及驗證碼驗證功能的實現(xiàn)詳解
- tp5(thinkPHP5框架)captcha驗證碼配置及驗證操作示例
- ThinkPHP5.0框架驗證碼功能實現(xiàn)方法【基于第三方擴(kuò)展包】
- thinkPHP5.0框架驗證碼調(diào)用及點擊圖片刷新簡單實現(xiàn)方法
- thinkPHP實現(xiàn)的驗證碼登錄功能示例
- thinkphp3.2實現(xiàn)在線留言提交驗證碼功能
- ThinkPHP實現(xiàn)生成和校驗驗證碼功能
- thinkPHP中驗證碼的簡單實現(xiàn)方法
- ThinkPHP5&5.1實現(xiàn)驗證碼的生成、使用及點擊刷新功能示例
相關(guān)文章
Netbeans 8.2與PHP相關(guān)的新特性介紹
Netbeans 8.2在10月1日國慶節(jié)發(fā)布了,下面通過本文給大家介紹與php相關(guān)的新特征,一起看看吧2016-10-10
PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】
這篇文章主要介紹了PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫,結(jié)合實例形式分析了基于thinkPHP5.1框架使用ODBC連接SQL Server2008數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
PHP+Memcache實現(xiàn)wordpress訪問總數(shù)統(tǒng)計(非插件)
這篇文章主要介紹了PHP+Memcache實現(xiàn)wordpress訪問總數(shù)統(tǒng)計,直接寫在主題functions.php中,并非實現(xiàn)的一個插件,需要的朋友可以參考下2014-07-07
使用xampp搭建運行php虛擬主機(jī)的詳細(xì)步驟
這篇文章主要介紹了通過使用xampp搭建運行php虛擬主機(jī)的步驟,介紹很詳細(xì),感興趣的小伙伴們可以參考一下2015-10-10
國產(chǎn)PHP開發(fā)框架myqee新手快速入門教程
這篇文章主要介紹了國產(chǎn)PHP開發(fā)框架myqee新手快速入門教程,myqee中文名稱邁啟PHP框架,有比較多的高級開發(fā)特性,需要的朋友可以參考下2014-07-07
laravel5.1框架model類查詢的實現(xiàn)方法
今天小編就為大家分享一篇laravel5.1框架model類查詢的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

