[eluser]Unknown[/eluser]
application/libraries/MY_Form_validation.php
Code:
<?php
class MY_Form_validation extends CI_Form_validation
{
function __construct()
{
parent::__construct();
}
public function captcha()
{
// First, delete old captchas
$expiration = time()-7200; // Two hour limit
$this->CI->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->CI->input->ip_address(), $expiration);
$query = $this->CI->db->query($sql, $binds);
$row = $query->row();
if ($row->count == 0)
{
$this->set_message('captcha', lang('captcha_err'));
return FALSE;
}else{
return TRUE;
}
}
function get_captcha()
{
$vals = array(
'word' => '',
'img_path' => 'static/captcha/',
'img_url' => base_url().'static/captcha/',
'font_path' => './path/to/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);
$cap = create_captcha($vals);
$data = array(
'captcha_time' => $cap['time'],
'ip_address' => $this->CI->input->ip_address(),
'word' => $cap['word']
);
$query = $this->CI->db->insert_string('captcha', $data);
$this->CI->db->query($query);
return $cap['image'];
}
}
in controller
Code:
class Welcome extends CI_Controller {
$this->load->library('form_validation');
$this->form_validation->set_rules('captcha', lang('captcha'), 'required|captcha');
if($this->form_validation->run() == TRUE)
{
// validate ok
}else{
// add image to data to pass in view
$data['captcha'] = $this->form_validation->get_captcha();
}
$this->load->view('welcome_message', $data);
}
in view
Code:
<?php echo $captcha?>