完整的例子

以下是使用資料庫的示例。在將顯示 CAPTCHA 的頁面上,你將擁有以下內容:

$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->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);  

// 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";
}