Welcome Guest, Not a member yet? Register   Sign In
Captcha Validation
#1

[eluser]ivains[/eluser]
Hello everyone.

I have created a registration form with validation which works except for the captcha validation. If the wrong code is placed it still processes the form. The plugin is being autoloaded. Also using database. Here is what I have.

Controller Code:
Code:
class Registration extends Controller {

    function Registration()
    {
        parent::Controller();

        $this->view_data['base_url'] = base_url();
        $this->load->model('registration_model');
    }

    // loads, validates, inputs and sends message of the new registrant
    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('captcha', 'Captcha', 'trim|required');

        // First, delete old captchas
        $expiration = time()-3600; // One hour limit
        $this->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($this->input->post('captcha'), $this->input->ip_address(), $expiration);
        $query = $this->db->query($sql, $binds);
        $row = $query->row();

                if ($this->form_validation->run() == FALSE)
                {
                    // if ceratin fields are not valid returns to form
                    $this->load->view('members/registration_form');
                }
                elseif ($row->count == 0)
                {
                echo 'You must submit the word that appears in the image.';
                }
                else
                {
                    // if all is alright processes the form and displays success page

Left out the other fluf here to shorten code a bit.

Views Code:
Code:
$vals = array(
    'img_path'     => './captcha/',
    'img_url'     => base_url().'captcha/',
    'font_path'     => './captcha/img/TOPSEO__.TTF',
    'img_width'     => '180',
    'img_height' => 40,
    'expiration' => 3600
);

    $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 '<label><font color="red">*</font>In order to finish your registration, please enter the word below:</label><br />';

echo $cap['image'];

echo '&lt;input type="text" name="captcha" value="" /&gt;';


Any suggestions and/or recommendations would be gratefully appreciated.

Thanks
Ivan
#2

[eluser]ivains[/eluser]
[Solved]

Found code in other post after digging for a week.

Changed:

$this->form_validation->set_rules('captcha', 'Captcha', 'trim|required');

To:

$this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_captcha_check[captcha]');




Theme © iAndrew 2016 - Forum software by © MyBB