CodeIgniter 驗證碼輔助函數(shù)

2018-07-21 15:43 更新

驗證碼輔助函數(shù)

驗證碼輔助函數(shù)文件包含了一些幫助你創(chuàng)建驗證碼圖片的函數(shù)。

加載輔助函數(shù)

該輔助函數(shù)通過下面的代碼加載:

$this->load->helper('captcha');

使用驗證碼輔助函數(shù)

輔助函數(shù)加載之后你可以像下面這樣生成一個驗證碼圖片:

$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'];
  • 驗證碼輔助函數(shù)需要使用 GD 圖像庫。
  • 只有 img_path 和 img_url 這兩個參數(shù)是必須的。
  • 如果沒有提供 word 參數(shù),該函數(shù)將生成一個隨機的 ASCII 字符串。 你也可以使用自己的詞庫,從里面隨機挑選。
  • 如果你不設(shè)置 TRUE TYPE 字體(譯者注:是主要的三種計算機矢量字體之一)的路徑,將使用 GD 默認的字體。
  • "captcha" 目錄必須是可寫的。
  • expiration 參數(shù)表示驗證碼圖片在刪除之前將保留多久(單位為秒),默認保留 2 小時。
  • word_length 默認值為 8, pool 默認值為 '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  • font_size 默認值為 16,GD 庫的字體對大小有限制,如果字體大小需要更大一點的話可以設(shè)置一種 TRUE TYPE 字體。
  • img_id 將會設(shè)置為驗證碼圖片的 "id" 。
  • colors 數(shù)組中如果有某個顏色未設(shè)置,將使用默認顏色。

添加到數(shù)據(jù)庫

使用驗證碼函數(shù)是為了防止用戶胡亂提交,要做到這一點,你需要將 create_captcha() 函數(shù)返回的信息保存到數(shù)據(jù)庫中。 然后,等用戶提交表單數(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ù)庫的示例。在顯示驗證碼的那個頁面,你的代碼類似于下面這樣:

$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="" />';

然后在處理用戶提交的頁面,處理如下:

// 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ù)有下列可用函數(shù):

create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])

參數(shù):

  • $data (array) -- Array of data for the CAPTCHA
  • $img_path (string) -- Path to create the image in
  • $img_url (string) -- URL to the CAPTCHA image folder
  • $font_path (string) -- Server path to font

返回: array('word' => $word, 'time' => $now, 'image' => $img)

返回類型: array

根據(jù)你提供的一系列參數(shù)生成一張驗證碼圖片,返回包含此圖片信息的數(shù)組。

array(
    'image' => IMAGE TAG
    'time'  => TIMESTAMP (in microtime)
    'word'  => CAPTCHA WORD
)

image 就是一個 image 標簽:

<img src="https://atts.w3cschool.cn/attachments/image/cimg/12345.jpg" width="140" height="50" />

time 是一個毫秒級的時間戳,作為圖片的文件名(不帶擴展名)。就像這樣:1139612155.3422

word 是驗證碼圖片中的文字,如果在函數(shù)的參數(shù)中沒有指定 word 參數(shù),這將是一個隨機字符串。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號