本文實(shí)例講述了CI框架教程之優(yōu)化驗證碼機(jī)制。分享給大家供大家參考,具體如下:
驗證碼機(jī)制在CI框架中是通過一個輔助函數(shù)captcha()進(jìn)行實(shí)現(xiàn)的——驗證碼輔助函數(shù)文件包含了一些幫助你創(chuàng)建驗證碼圖片的函數(shù)。。
那么我們?nèi)绾问褂肅I的captcha()輔助函數(shù)來完成驗證碼功能呢?下面我會先講述如何使用CI的captcha()輔助函數(shù)來完成驗證碼功能,然后在講述如何具體的對CI框架的驗證碼機(jī)制進(jìn)行優(yōu)化。
1、CI框架驗證碼功能的使用
a) 首先我們要先加載輔助函數(shù)
加載輔助函數(shù)一共有兩種方法:
①、自動加載
我們可以在根文件目錄下的 “application/config/autoload.php” 文件中進(jìn)行設(shè)置自動加載。
1
2
3
|
//ci框架設(shè)置自動加載輔助函數(shù) //captcha驗證碼復(fù)制函數(shù) $autoload [ 'helper' ] = array ( 'url' , 'captcha' ); |
由于我們的項目使用驗證碼的地方非常有限,故而不推薦使用自動加載這種方法,我們可以在使用到的地方加載使用就可以了。
②、在使用到的地方進(jìn)行加載
這種方法我們還是比較推薦的,消耗資源較少,效率會稍微的高一點(diǎn)。在你使用到驗證碼的控制器中寫一個構(gòu)造函數(shù),在構(gòu)造函數(shù)中進(jìn)行驗證碼輔助函數(shù)加載就可以了。
1
2
3
4
5
6
|
//構(gòu)造函數(shù) public function __construct() { //切記在控制器的構(gòu)造函數(shù)中一定先繼承父類控制器的構(gòu)造函數(shù) parent::__construct(); $this ->load->helper( 'captcha' ); } |
b) 然后使用驗證碼輔助函數(shù)創(chuàng)建驗證碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
$vals = array ( 'word' => 'Random word' , //驗證碼上顯示的字符,可以寫成函數(shù),例如:rand(100000,999999) 'img_path' => './data/captcha/' , //驗證碼保存路徑 'img_url' => base_url( 'data/captcha' ), //驗證碼圖片url 'font_path' => './path/to/fonts/texb.ttf' , //驗證碼上字體 'img_width' => '150' , //驗證碼圖片寬度 'img_height' => 30, //驗證碼圖片高度 'expiration' => 7200, //驗證碼圖片刪除時間 'word_length' => 8, //驗證碼長度 'font_size' => 16, //驗證碼字體大小 'img_id' => 'Imageid' , 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' , 'colors' => array ( 'background' => array (255, 255, 255), 'border' => array (255, 255, 255), 'text' => array (0, 0, 0), 'grid' => array (255, 40, 40) ), ); $cap = create_captcha( $vals ); var_dump( $cap ); |
這樣驗證碼就創(chuàng)建完成,img_path和img_url這倆個參數(shù)必須存在,并且,img_path所表示的路徑文件夾必須存在,不然的話創(chuàng)建驗證碼不會成功。由于每創(chuàng)建一次驗證碼就會生成一張圖片放到你設(shè)置的文件夾中,這樣是非常消耗資源的,故此我們要對CI框架的驗證碼功能進(jìn)行優(yōu)化。
2、CI框架驗證碼的優(yōu)化
優(yōu)化思路:①、我們不讓框架生成的圖片進(jìn)行保存到服務(wù)器中;②、我們只保留驗證碼的的內(nèi)容即可。
要想對驗證碼功能進(jìn)行優(yōu)化,我們就要對驗證碼輔助函數(shù)功能進(jìn)行擴(kuò)展。
a) 擴(kuò)展驗證碼輔助函數(shù)
首先將根目錄下 “system/helpers/captcha_helper.php” 文件復(fù)制一份到根目錄下 "application/helpers" 目錄下,命名為 "MY_captcha_helper.php" ;
然后將下面代碼注釋掉(大概在96行到119行);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
if ( $img_path === '' OR $img_url === '' OR ! is_dir ( $img_path ) OR ! is_really_writable( $img_path ) OR ! extension_loaded ( 'gd' )) { return FALSE; } // ----------------------------------- // Remove old images // ----------------------------------- $now = microtime(TRUE); $current_dir = @opendir( $img_path ); while ( $filename = @readdir( $current_dir )) { if ( substr ( $filename , -4) === '.jpg' && ( str_replace ( '.jpg' , '' , $filename ) + $expiration ) < $now ) { @unlink( $img_path . $filename ); } } @ closedir ( $current_dir ); |
此段代碼防止你沒有傳遞img_path和img_url參數(shù)以及參數(shù)所指的文件夾不存在就暫停執(zhí)行函數(shù)。
再次注釋代碼(大概在318行到335行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$img_url = rtrim( $img_url , '/' ). '/' ; if (function_exists( 'imagejpeg' )) { $img_filename = $now . '.jpg' ; imagejpeg( $im , $img_path . $img_filename ); } elseif (function_exists( 'imagepng' )) { $img_filename = $now . '.png' ; imagepng( $im , $img_path . $img_filename ); } else { return FALSE; } $img = '<img ' .( $img_id === '' ? '' : 'id="' . $img_id . '"' ). ' src="' . $img_url . $img_filename . '" style="width: ' . $img_width . '; height: ' . $img_height . '; border: 0;" id="codetool">
此段代碼用于創(chuàng)建驗證碼圖片,并且將圖片保存到你說創(chuàng)建的驗證碼文件夾中(image_path)。
最后,在
b) 應(yīng)用擴(kuò)展優(yōu)化之后的驗證碼功能 首先在控制器中寫一個生成驗證碼方法; 然后在方法中進(jìn)行調(diào)用驗證碼輔助函數(shù),生成驗證碼; 最后在前臺進(jìn)行調(diào)用方法,并實(shí)現(xiàn)點(diǎn)擊刷新功能。 生成驗證碼函數(shù)代碼:
前臺調(diào)用餅實(shí)時刷新調(diào)用:
至此,CI框架的驗證碼功能機(jī)制優(yōu)化我們就完成了。 希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。 原文鏈接:https://blog.csdn.net/Zhihua_W/article/details/52524039 延伸 · 閱讀
精彩推薦
|