Welcome Guest, Not a member yet? Register   Sign In
reCaptcha - Problems fetching post values for reCaptcha
#1

[eluser]Unknown[/eluser]
Hi,

I'm pretty new to Codeigniter and have been trying to debug and figure out what is wrong with my reCaptcha implementation. Maybe someone here can help me?

Basically the problem is when I run the check_captcha() function I don't feed it with the values it wants; and I can't figure out why.

This is built on the reCaptcha implementation posted here in the forum/wiki

Please tell me if there is anything missing. Thankful for all help. I have attached my whole project.

NOTE: It works when using the nocode part of the implementation. But I don't want that.

reCaptcha-HTML:
Code:
[removed]
  var RecaptchaOptions = {
    theme:"<?= $theme ?>",
    lang:"<?= $lang ?>"
  };
[removed]
[removed][removed]
<noscript>
        &lt;iframe src="&lt;?= $server ?&gt;/noscript?lang=&lt;?= $lang ?&gt;&k=&lt;?= $key.$errorpart ?&gt;" height="300" width="500" frameborder="0"&gt;&lt;/iframe><br/>
        &lt;textarea name="recaptcha_challenge_field" rows="3" cols="40"&gt;&lt;/textarea>
        &lt;input type="hidden" name="recaptcha_response_field" value="manual_challenge"/&gt;
</noscript>

Login/signup controller
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function  __construct() {
        parent::__construct();
        $this->load->library('recaptcha');
        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->lang->load('recaptcha');
        

    }

    function index(){
        $data['title'] = "Moosic - Login";
        $data['heading'] = "Login to Moosic";

        $data['main_content'] = 'login_view';
        $this->load->view('includes/main_template', $data);
    }

    function validate_credentials() {
        // Load models
        $this->load->model('Users_model');

        $query = $this->Users_model->validate();

        if($query){ // if session validates
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => TRUE
            );

            $this->session->set_userdata($data);
            redirect('posts');

        } else {
            // TODO Add error message
            $this->index();
        }

    }

    function logout(){
        $this->session->sess_destroy();
        redirect('posts');
    }

    function signup(){
        $data['title'] = "Moosic - Signup";
        $data['heading'] = "Signup for Moosic";

        $data['recaptcha'] = array('html'=>$this->recaptcha->get_html());

        $data['main_content'] = 'login_signup_view';
        $this->load->view('includes/main_template', $data);
    }

    function signup_user(){

        // field name, error messages, validation rules
        $this->form_validation->set_rules('first_name', 'First name', 'trim|required|max_length[25]');
        $this->form_validation->set_rules('last_name', 'Last name', 'trim|required|max_length[25]');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email|max_length[50]');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[32]');
        $this->form_validation->set_rules('password_confirm', 'Confirm password', 'trim|required|matches[password]');
        
        // DEBUG LOG
        var_dump($this->input->post(NULL, FALSE));
        //log_message('debug','Recaptcha:$_POST Debug @before validation->run()   '.print_r($this->input->post(NULL, FALSE)).')');
        //------------
        
        // ReCaptcha
        $this->form_validation->set_rules('recaptcha_response_field', 'lang:recaptcha_field_name', 'callback_check_captcha');  
        
        if($this->form_validation->run() == FALSE){
            $this->signup();
        } else {
            $this->load->model('Users_model');

            if($query = $this->Users_model->create_user()){
                redirect('login');
            } else {
                $this->signup();
            }
        }
    }

    function check_captcha($str) {
         if ($this->recaptcha->check_answer($this->input->ip_address(),$this->input->post('recaptcha_challange_field'),$str)) {
            return TRUE;
          } else {
            $this->form_validation->set_message('check_captcha',$this->lang->line('recaptcha_incorrect_response'));
            return FALSE;
          }
    }
    
}

?&gt;
#2

[eluser]Unknown[/eluser]
Hi,

I managed to solve this after several hours of troubleshooting. Turned out to be a HTML issue rather than a PHP issue. It might actually have with the JAVA-script for ReCaptcha.

It turned out that I started a fieldset before I opened the form, like this:
Code:
...
<fieldset>
    <legend>Login information</legend>
    &lt;?php
        form_open('login/signup_user')

        echo "<p>" . form_label('Username', 'username') . "<br />" . form_input('username', set_value('username')) . "</p>";
        echo "<p>" . form_label('Password', 'password') . "<br />" . form_password('password', set_value('password')) . "</p>";
        echo "<p>" . form_label('Confirm password', 'password_confirm') . "<br />" . form_password('password_confirm', '') . "</p>";
    ?&gt;
</fieldset>
...

I changed to this, of course you have to open your form before you start your fieldset:
Code:
&lt;?= form_open('login/signup_user') ?&gt;

<fieldset>
    <legend>Login information</legend>
    &lt;?php
        echo "<p>" . form_label('Username', 'username') . "<br />" . form_input('username', set_value('username')) . "</p>";
        echo "<p>" . form_label('Password', 'password') . "<br />" . form_password('password', set_value('password')) . "</p>";
        echo "<p>" . form_label('Confirm password', 'password_confirm') . "<br />" . form_password('password_confirm', '') . "</p>";
    ?&gt;
</fieldset>

Problem solved! Thanks to anyone that tried to help me but did not beat me to it. I used...
Code:
var_dump($this->input->post(NULL, FALSE));
...to debug and I finally used the original demo code to look at the problem and started cutting and pasting from my code to the demo code. The strange thing is that all $_POST-data was sent except the one from ReCaptcha.




Theme © iAndrew 2016 - Forum software by © MyBB