完整的例子

以下是使用数据库的示例。在将显示 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";
}