W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
驗(yàn)證碼輔助函數(shù)文件包含了一些幫助你創(chuàng)建驗(yàn)證碼圖片的函數(shù)。
該輔助函數(shù)通過(guò)下面的代碼加載:
$this->load->helper('captcha');
輔助函數(shù)加載之后你可以像下面這樣生成一個(gè)驗(yàn)證碼圖片:
$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'];
使用驗(yàn)證碼函數(shù)是為了防止用戶胡亂提交,要做到這一點(diǎn),你需要將 create_captcha() 函數(shù)返回的信息保存到數(shù)據(jù)庫(kù)中。 然后,等用戶提交表單數(shù)據(jù)時(shí),通過(guò)數(shù)據(jù)庫(kù)中保存的數(shù)據(jù)進(jìn)行驗(yàn)證,并確保它沒(méi)有過(guò)期。
這里是數(shù)據(jù)表的一個(gè)例子:
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ù)庫(kù)的示例。在顯示驗(yàn)證碼的那個(gè)頁(yè)面,你的代碼類似于下面這樣:
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => 'http://example.com/captcha/'
);
$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="" />';
然后在處理用戶提交的頁(yè)面,處理如下:
// 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.';
}
該輔助函數(shù)有下列可用函數(shù):
create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])
參數(shù):
返回: array('word' => $word, 'time' => $now, 'image' => $img)
返回類型: array
根據(jù)你提供的一系列參數(shù)生成一張驗(yàn)證碼圖片,返回包含此圖片信息的數(shù)組。
array(
'image' => IMAGE TAG
'time' => TIMESTAMP (in microtime)
'word' => CAPTCHA WORD
)
image 就是一個(gè) image 標(biāo)簽:
<img src="https://atts.w3cschool.cn/attachments/image/cimg/12345.jpg" width="140" height="50" />
time 是一個(gè)毫秒級(jí)的時(shí)間戳,作為圖片的文件名(不帶擴(kuò)展名)。就像這樣:1139612155.3422
word 是驗(yàn)證碼圖片中的文字,如果在函數(shù)的參數(shù)中沒(méi)有指定 word 參數(shù),這將是一個(gè)隨機(jī)字符串。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: