[eluser]mi6crazyheart[/eluser]
Hello Guys
Today, i got a Mysql error from the CI Captcha validation script which has given in " captcha_pi.php " file. Here is portion of code i'm showing here to show you.
Code:
Here is a table prototype:
CREATE TABLE captcha (
captcha_id bigint(13) unsigned NOT NULL auto_increment,
captcha_time int(10) unsigned NOT NULL,
ip_address varchar(16) default '0' NOT NULL,
word varchar(20) NOT NULL,
PRIMARY KEY `captcha_id` (`captcha_id`),
KEY `word` (`word`)
)
Here is an example of usage with a DB.
On the page where the captcha will be shown you'll have something like this:
$this->load->plugin('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => 'http://example.com/captcha/'
);
$cap = create_captcha($vals);
$data = array(
'captcha_id' => '',
'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="" />';
Then, on the page that accepts the submission you'll have something like this:
// First, delete old captchas
$expiration = time()-7200; // Two hour limit
$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 date > ?";
$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";
}
Here, when we are creating that "captcha"(line-90 from captcha_pi.php) table we are not creating any "date" field. We are only creating 4 fields. These are "captcha_id, captcha_time, ip_address, word" . But, at the validation time when we are checking for any existing captcha by this query.....
Code:
// Then see if a captcha exists:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date > ?"; (line-134 from captcha_pi.php)
we are searching for "date" field. But, i think it should be "captcha_time" field. Check it u'r self once again. But, now my script is running well after i made that correction.
Once more, error i got from this portion of code is "undefined variable DB" from this line
Code:
$DB->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); (line-131 from captcha_pi.php)
Here what i changed is : replace "$DB" by "$this->db" . After this my script is working well.