Welcome Guest, Not a member yet? Register   Sign In
integrate form validation and captcha helper
#1

[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:
&lt;?php echo $captcha?&gt;


Messages In This Thread
integrate form validation and captcha helper - by El Forum - 01-18-2012, 07:00 AM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 04:59 AM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 06:26 AM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 08:29 AM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 11:52 AM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 11:55 AM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 03:39 PM
integrate form validation and captcha helper - by El Forum - 06-25-2014, 11:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB