Skip to content

Instantly share code, notes, and snippets.

@nolochemical
Created November 12, 2012 14:03
Show Gist options
  • Save nolochemical/4059566 to your computer and use it in GitHub Desktop.
Save nolochemical/4059566 to your computer and use it in GitHub Desktop.
CI v2.1.3 Captcha Snippet
// captcha helper
// _CI_DIR_v2.1.3_/user_guide/helpers/captcha_helper.html
// Keep in mind your binding vars right *into your db query*, double check your inputs.
--controller : form_validation, callback__check_cap
function _check_cap($str)
{
if( $this->ap->clean_captcha($str) == TRUE)
{
return TRUE;
}
else
{
$this->form_validation->set_message('_check_cap', 'The code your entered was incorrect, please try again.');
return FALSE;
}
}
--model
//
function clean_captcha($str)
{
$expiration = time()-900; // ~15 minutes is fine for testing
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
// Then see if a captcha exists:
/* The font might hard to read so loosen up on case-sensitivity; hence the `LIKE` instead of `=`
*/
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word LIKE ? AND ip_address = ? AND captcha_time > ?";
$binds = array($str, $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();
if ($row->count == 0)
{
return FALSE;
}
else if($row->count >0)
{
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment