0
votes

I am at my wits end and don't know why my code is not working . Everything seems good to me .. I have implemented a captcha in my user review function and added a verification method using callback_ .

The captcha is showing in the view and i have dumped the session data and the input field data and they are both working.

The form validation is also working in case of captcha input field but seems like the callback_check_captcha parameter is not working but the function seems fine to me .

Here is my controller

function user_review($id = null , $start = 0){


    $check_id = $this->mdl_phone->get_phone_feature($id);
    if ($check_id == null ) {
        $data['phone_model']    =   $this->get_phone_models();
        $data['feature'] = null;
        $data['title']      =   'Nothing Found';
        $data['main_content']= 'phone/error';
        echo Modules::run('templates/main',$data);
    } else{

        $data['phone_model']    =   $this->get_phone_models();

        $data['success'] = null;
        $data['errors'] = null;

        if($this->input->server("REQUEST_METHOD") === 'POST' ){

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

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

            if($this->form_validation->run() == FALSE){
                $data['errors'] = validation_errors();
            }else{

                $user = $this->ion_auth->user()->row();
                $user_id = $user->id;

                $data = array(
                        'phone_id'  => $id,
                        'user_id'   => $user_id,
                        'text'      => strip_tags($this->input->post('text')),
                );

                $this->db->insert('user_review' ,$data);
                $data['phone_model']    =   $this->get_phone_models();
                $data['success'] = 'Your Review has been successfully posted ';
                $data['errors']= null;
            }
        }


        // Initilize all Data at once by $id
        $data['feature']        =   $this->mdl_phone->get_phone_feature($id);
        //$data['rating']           =   $this->mdl_phone->get_user_rating($id);
        $data['user_review']    =   $this->mdl_phone->get_user_review($id , 5 , $start);

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

        $config['base_url']     =   base_url().'phone/user_review/'.$id;
        $config['total_rows']   =   $this->mdl_phone->get_user_review_count($id);
        $config['per_page']     =   5;
        $config['uri_segment']  =   4;
        $config['anchor_class'] =   'class="page" ';

        $this->pagination->initialize($config);

        $this->load->helper('captcha');

        $vals = array(
                'img_path' => './captcha/',
                'img_url' => base_url().'captcha/',
                'img_width' => 150,
                'img_height' => 30,
        );

        $cap = create_captcha($vals);
        $this->session->set_userdata('captcha',$cap['word']);

        $data['captcha']        = $cap['image'];

        $data['title']          = $this->mdl_phone->get_phone_title($id)." User Review , Rating and Popularity";
        $data['main_content']   = 'phone/user_review';
        echo Modules::run('templates/main',$data);
    }
}

function check_captcha($cap)
{
    if($this->session->userdata('captcha') == $cap )
    {   
        return true;

    }
    else{
        $this->form_validation->set_message('check_captcha', 'Security number does not match.');
        return false;
    }
}
1

1 Answers

0
votes

The bellow CAPTHA working properly refer this code

$this->load->helper('captcha');
    $vals = array(
        'img_path' => './captcha/',
        'img_url' => base_url() . '/captcha/'
    );
    $cap = create_captcha($vals);
    $data = array(
        'captcha_time' => $cap['time'],
        'ip_address' => $this->input->ip_address(),
        'word' => $cap['word']
    );
    $this->session->set_userdata($data);
    $data['cap_img'] = $cap['image'];

I think your image url problem or you are not giving a file permission to the captha folder .