Created
November 12, 2012 14:03
-
-
Save nolochemical/4059566 to your computer and use it in GitHub Desktop.
CI v2.1.3 Captcha Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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