CodeIgniter Forums
Captcha helper problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Captcha helper problem (/showthread.php?tid=43228)

Pages: 1 2


Captcha helper problem - El Forum - 07-05-2011

[eluser]SaSa[/eluser]
Code:
// validation captcha //
$captcha = $this->input->post('captcha');

    // First, delete old captchas
    $expiration = time()-900; // Two 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($captcha, $this->input->ip_address(), $expiration);
    $row = $this->db->query($sql, $binds)->row();
    
    if ($row->count == 0)
    {
        return false;
        $captcha_query = "0";
        
    }else {
        return true;
        $captcha_query = "1";
    }

Validation captcha not work, is always return true. variables $captcha may or not may, not distinction. use of CodeIgniter 2.0.2
What is the solution?


Captcha helper problem - El Forum - 07-05-2011

[eluser]SaSa[/eluser]
Please guide me...


Captcha helper problem - El Forum - 07-05-2011

[eluser]SaSa[/eluser]
Why does not anyone help?


Captcha helper problem - El Forum - 07-05-2011

[eluser]mi6crazyheart[/eluser]
Don't know how u doing the things. But, i think this thing will help u...

Controller part
----------------
Code:
$this->form_validation->set_rules('securityCode', 'Security Code', 'trim|required|callback_Captcha_check');

//-----Call back function for checking given captcha code is valid or not-----
        function Captcha_check($str)
        {
            // First, delete old captchas
            $expiration = time()-7200; // Two hour limit
            // $DB->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);        
            $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('securityCode'), $this->input->ip_address(), $expiration);
            $query = $this->db->query($sql, $binds);
            $row = $query->row();
        
            if ($row->count == 0){
                $this->form_validation->set_message('Captcha_check', 'Security code miss match. Try again');
                return FALSE;
                //echo "You must submit the word that appears in the image";
            }            
        }


VIEW part
-------------
Code:
$vals = array(
            'word'            => '',
            'img_path'        => './captcha/',
            //'img_url'        => 'http://localhost/vb/captcha/',
            'img_url'        => base_url().'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->input->ip_address(),
                'word'             => $cap['word']
            );

            $query = $this->db->insert_string('captcha', $data);
            $this->db->query($query);

            echo $cap['image'];

Hope this much will help...


Captcha helper problem - El Forum - 07-05-2011

[eluser]SaSa[/eluser]
I am sorry, dont know much speak english.
i want return or variable send to php code but return is always true to my code?


Captcha helper problem - El Forum - 07-05-2011

[eluser]mi6crazyheart[/eluser]
Try to use this piece of code instead of u'r code...

Code:
// Then see if a captcha exists:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
$binds = array($captcha, $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();



Captcha helper problem - El Forum - 07-05-2011

[eluser]SaSa[/eluser]
not working.!!!!!!!
i use of this codes (post #0) to model.


Captcha helper problem - El Forum - 07-05-2011

[eluser]mi6crazyheart[/eluser]
Can u show u'r VIEW page code & CONTROLLER code...


Captcha helper problem - El Forum - 07-05-2011

[eluser]InsiteFX[/eluser]
It always returns TRUE because of your code here:
Code:
if ($row->count == 0)
    {
        return false;
        $captcha_query = "0";
        
    }else { // will always return TRUE because its getting a database table row!
        return true;
        $captcha_query = "1";
    }

Which means one of your querys is wrong!

InsiteFX


Captcha helper problem - El Forum - 07-06-2011

[eluser]SaSa[/eluser]
ok.my codes:

CI_Controller:
Code:
function index()
    {
    if($this->session->userdata('login') || $this->session->userdata('logged'))
        {
            redirect('admin/adminindex');
        }else {
    //************** check validation **************//
    $useradmin = $this->input->post('useradmin');
    $passadmin = $this->input->post('passadmin');
    $captcha = $this->input->post('captcha');    
        //If succses fulll fild        
            if($this->login_model->validation_login($useradmin, $passadmin, $captcha))
            {
                $data = array('login' => $useradmin, 'logged'=>true);
                $this->session->set_userdata($data);
                redirect('admin/adminindex');
            }
            else
        {
        $this->session->cookie_monster($this->input->post('remember_me') ? FALSE : TRUE);
        //************** create captcha **************//
        $vals = array(
            'img_path'     => './captcha/',
            'img_url'     => 'captcha/',
        );
        $cap = create_captcha($vals);
        $data['cap'] = $cap;
        $this->login_model->create_captcha($cap);
        
                $this->load->view('admin/login', $data);
            }
        }
    }


CI_Model:
Code:
function validation_login($useradmin, $passadmin, $captcha)
    {
    // validation useradmin //
        $query = $this->db->get_where('login', array('useradmin' => $useradmin));
    if ($query->num_rows()==1) {
        return true;
        $user_query  = '1';
    } else {
        return false;
        $user_query = '0';        
    }
    
    // validation passadmin //
    $query = $this->db->get_where('login', array('passadmin' => sha1($passadmin)));
    if ($query->num_rows()==1) {
        return true;
        $pass_query = '1';
    } else {
        return false;
        $pass_query = '0';
    }
    
    // validation captcha //
    // First, delete old captchas
    $expiration = time()-900; // Two 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($captcha, $this->input->ip_address(), $expiration);
    $query = $this->db->query($sql, $binds);
    $row = $query->row();
    if ($row->count == 0)
    {
        echo 'not ok';
        $captcha_query = "0";
        return FALSE;
    }else {
        echo 'not ok';
        $captcha_query = "1";
        return TRUE;
        
    }
    
    return json_encode($user_query.$pass_query.$captcha_query);
    }

view:
Code:
&lt;form action="&lt;?php echo base_url();?&gt;login" method="post" accept-charset="utf-8" &gt;
                            &lt;input type="text" name="useradmin" value="" id="input_username" placeholder="username" /&gt;
                            <div class="result_username"></div>
                            <br/>
                            &lt;input type="password" name="passadmin" value="" id="input_password" placeholder="password" /&gt;
                            <div class="result_password"></div>
                            <br/>                            
                            <div id="capcher_code">&lt;?= $cap['image'];?&gt;</div>
                            &lt;input type="text" name="captcha" value="" id="input_code" /&gt;
                            <div class="result_capcher"></div>
                            &lt;input type="submit" value="" id="login_submit" /&gt;                        
&lt;/form&gt;