?
This document uses PHP Chinese website manual Release
CAPTCHA幫助程序文件包含幫助創(chuàng)建CAPTCHA圖像的功能。
加載這個幫手
使用CAPTCHA助手
添加數(shù)據(jù)庫
可用功能
這個幫助器使用下面的代碼加載:
$this->load->helper('captcha');
一旦加載,你可以生成這樣的驗證碼:
$vals = array( 'word'=> 'Random word', 'img_path'=> './captcha/', 'img_url' => 'http://example.com/captcha/', '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', // White background and border, black text and red grid '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); echo $cap['image'];
驗證碼功能需要GD圖像庫。
只有img_path和img_url是必需的。
如果沒有提供一個單詞,該函數(shù)將生成一個隨機的ASCII字符串。你可以把你自己的詞庫放在一起,你可以從中隨機抽取。
如果不指定TRUE TYPE字體的路徑,則將使用本機丑陋的GD字體。
“captcha”目錄必須是可寫的
的到期(秒)表示的圖像將多久保留在驗證碼文件夾之前將被刪除。默認值是兩個小時。
word_length默認為8,池默認為'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
font_size默認為16,原生GD字體的大小限制。指定更大尺寸的“真實類型”字體。
該img_id將被設置為驗證碼圖像的“身份證”。
如果任何顏色值丟失,它將被替換為默認值。
為了使驗證碼功能防止某人提交,您需要添加從create_captcha()
數(shù)據(jù)庫返回的信息。然后,當用戶提交表單中的數(shù)據(jù)時,您需要驗證數(shù)據(jù)是否存在于數(shù)據(jù)庫中并且沒有過期。
這是一個表格原型:
CREATE TABLE captcha ( captcha_id bigint(13) unsigned NOT NULL auto_increment, captcha_time int(10) unsigned NOT NULL, ip_address varchar(45) NOT NULL, word varchar(20) NOT NULL, PRIMARY KEY `captcha_id` (`captcha_id`), KEY `word` (`word`));
這是一個數(shù)據(jù)庫使用的例子。在顯示CAPTCHA的頁面上,您會看到如下所示的內(nèi)容:
$this->load->helper('captcha'); $vals = array( 'img_path' => './captcha/', 'img_url' => ' $cap = create_captcha($vals); $data = array( 'captcha_time' => $cap['time'], 'ip_address' => $this->input->ip_address(), 'word' => $cap['word']); $query = $this->db->insert_string('captcha', $data); $this->db->query($query); echo 'Submit the word you see below:'; echo $cap['image']; echo '<input type="text" name="captcha" value="" />';
然后,在接受提交的頁面上,您將擁有如下所示的內(nèi)容:
// First, delete old captchas $expiration = time() - 7200; // Two hour limit $this->db->where('captcha_time < ', $expiration)->delete('captcha'); // Then see if a captcha exists: $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?'; $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration); $query = $this->db->query($sql, $binds); $row = $query->row();if ($row->count == 0){ echo 'You must submit the word that appears in the image.'; }
以下功能可用:
create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])
參數(shù): | $ data(array) - 用于CAPTCHA的數(shù)據(jù)數(shù)組$ img_path(字符串) - 在$ img_url中創(chuàng)建圖像的路徑(字符串) - CAPTCHA圖像文件夾的URL $ font_path(字符串) - 字體的服務器路徑 |
---|---|
返回: | 數(shù)組('word'=> $ word,'time'=> $ now,'image'=> $ img) |
返回類型: | 排列 |
$ data(array) - CAPTCHA的數(shù)據(jù)數(shù)組
$ img_path(字符串) - 在中創(chuàng)建圖像的路徑
$ img_url(字符串) - CAPTCHA圖像文件夾的URL
$ font_path(字符串) - 字體的服務器路徑
Returns: array(‘word’ => $word, ‘time’ => $now, ‘image’ => $img)
返回類型:數(shù)組
獲取一系列信息以生成CAPTCHA作為輸入,并根據(jù)您的規(guī)范創(chuàng)建圖像,并返回關于圖像的一組關聯(lián)數(shù)據(jù)。
array( 'image' => IMAGE TAG 'time' => TIMESTAMP (in microtime) 'word' => CAPTCHA WORD )
該圖像是實際的圖像標簽:
<img src =“http://example.com/captcha/12345.jpg”width =“140”height =“50”/>
的時間是用作不文件擴展名的圖像名稱微時間戳。這將是一個這樣的數(shù)字:1139612155.3422
的字是在出現(xiàn)的驗證碼圖像,其中,如果未提供的功能,可以是隨機字符串中的字。