1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
7 private $params = NULL;
9 public function __construct()
11 $this->ci =& get_instance();
12 $this->ci->config->load('captcha');
13 $this->ci->load->library('Singleton_db');
14 $this->db = $this->ci->singleton_db->connect();
16 $this->params = $this->ci->config->item('captcha_params');
19 die('Cannot load CAPTCHA config file.');
23 * Generates a CAPTCHA image and returns an HTML image tag for it.
28 public function get_captcha_tag($word = NULL)
30 $this->load->helper('captcha');
33 $this->params['word'] = $word;
35 $cap = create_captcha($this->params);
38 'captcha_time' => $cap['time'],
39 'ip_address' => $this->input->ip_address(),
40 'word' => $cap['word']
43 $str_query = $this->db->insert_string('captcha', $data);
44 $this->db->query($str_query);
50 * Check againt the DB if the word(s) entered by the user ($word) matches
51 * the CAPTCHA and if the CAPTCHA did not expired.
53 public function check_captcha($word)
55 // First, delete old captchas
56 $expiration_limit = (!$this->params['expiration']
57 ? 7200 : $this->params['expiration']);
58 $expiration = time() - $expiration_limit; // Two hour limit
59 $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
60 // TODO also delete the CAPTCHA image file
62 // Then see if a captcha exists:
63 $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
64 $binds = array($word, $this->input->ip_address(), $expiration);
65 $query = $this->db->query($sql, $binds);
77 /* End of file Captcha.php */
78 /* Location: ./application/libraries/Captcha.php */