Frage

I tried to use this RECaptcha-for-Codeigniter for one of my old site, the coding is very old one.

The Re-captcha is not loading in my page.

I have used this one : https://github.com/Cnordbo/RECaptcha-for-Codeigniter

The Code For : /application/modules_core/register/controllers/register.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class register extends MX_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('recaptcha');
        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->load->library('session');
        $this->load->model('index/user_model');
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
        $data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();
    }

        public function index()
        {       
            $Add_one=$this->input->post('Add_one');
            $Add_two2=$this->input->post('Add_two2');
            $Email_id=$this->input->post('Email_id');
            $Password=base64_encode($this->input->post('Password'));
            $city=$this->input->post('city');
            $country=$this->input->post('country');
            $f_nmae=$this->input->post('f_nmae');
            $fax_no=$this->input->post('fax_no');
            $m_nmae=$this->input->post('m_nmae');
            $l_nmae=$this->input->post('l_nmae');
            $mob_no=$this->input->post('mob_no');
            $phn_no=$this->input->post('phn_no');
            $state=$this->input->post('state');
            $user_name=$this->input->post('user_name');
            $zip=$this->input->post('zip');
            $company=$this->input->post('company');
            //$type=$this->input->post('type');
            $type=4;
            $submit=$this->input->post('submit');


        $data1 = array(
            'add_one'=>$Add_one,
            'add_two2'=>$Add_two2,
            'email_id'=>$Email_id,
            'password'=>$Password,
            'city'=>$city,
            'country'=>$country,
            'f_nmae'=>$f_nmae,
            'fax_no'=>$fax_no,
            'm_nmae'=>$m_nmae,
            'l_nmae'=>$l_nmae,
            'mob_no'=>$mob_no,
            'phn_no'=>$phn_no,
            'state'=>$state,
            'user_level_id'=>$type,
            'user_name'=>$user_name,
            'zip'=>$zip,
            'company'=>$company
        );

        $this->form_validation->set_rules('f_nmae', 'First Name', 'required');
        $this->form_validation->set_rules('l_nmae', 'Last Name', 'required');
        $this->form_validation->set_rules('Add_one', 'Addresss line 1', 'required');
        $this->form_validation->set_rules('phn_no', 'phone #', 'required');
        $this->form_validation->set_rules('fax_no', 'Fax #', 'required');
        $this->form_validation->set_rules('company', 'Company', 'trim|required');
        $this->form_validation->set_rules('Email_id', 'Email', 'required|valid_email|is_unique[user.email_id]');
        $this->form_validation->set_rules('user_name', 'User Name', 'required|is_unique[user.user_name]');
        $this->form_validation->set_rules('Password', 'Password', 'required');
        $this->form_validation->set_rules('confrm_Password', 'Confirm Password', 'required|matches[Password]');

        if ($this->form_validation->run() == FALSE)
                        {
                        $this->load->view('index/header'); 
                        $this->load->view('content',$data);  
                        $this->load->view('index/footer');
                        }
                    else
                    {
                            if($submit=='submit')
                            {
                                $this->load->library('recaptcha');
                                $this->recaptcha->recaptcha_check_answer();

                                if(!$this->recaptcha->getIsValid()) {
                                        $this->session->set_flashdata('error','incorrect captcha');
                                    } else {
                                         $this->session->set_flashdata('error','incorrect credentials');
                                }

                                $this->user_model->insert_user($data1);
                                $data['success']=1;
                                $this->load->library('email');
                                $config['mailtype'] = 'html';
                                $this->email->initialize($config);
                                $this->email->from('registration@ontimerecords.com');
                                $this->email->to('mitch@ontimerecords.com');
                                $this->email->subject('Ontimerecords New User Registration');
                                $mesg = "Ontimerecords New User Registration Details<br><br>";
                                $mesg .="First Name: ".$data1['f_nmae']."<br><br>";
                                $mesg .="Middle Name: ".$data1['m_nmae']."<br><br>";
                                $mesg .="Last Name: ".$data1['l_nmae']."<br><br>";
                                $mesg .="Company : ".$data1['company']."<br><br>";
                                $mesg .="Address : ".$data1['add_one']."<br><br>";
                                $mesg .="City, State, Zip : ".$data1['zip']."<br><br>";
                                $mesg .="Phone : ".$data1['phn_no']."<br><br>";
                                $mesg .="Email : ".$data1['email_id']."<br><br>";
                                $mesg .="Fax : ".$data1['fax_no']."<br><br>";
                                $mesg .="User Name : ".$data1['user_name']."<br><br>";
                                $mesg .="<br><br><br>Regards, <br><br>Ontimerecords";
                                $this->email->message($mesg);
                                $this->email->send();

                            }
                    $this->load->view('index/header'); 
                    $this->load->view('content',$data);  
                    $this->load->view('index/footer');}
                    }
        }

The Code For : /application/modules_core/register/views/content.php

 <div class="content">
      <div class="mid-content">
        <h1 style="margin-left:20px; color:#4C4C4C; ">User Registration</h1>
        <?php
        if(isset($success))
            {
                echo '<div style="margin-left:20px; color:#ec449b"><h3>You are Registered Successfully.</h3></div>';
            }
        ?>
        <div class="left_panel_full" id="left_panel_2" style="border:0px solid #F00;">
          <div class="list_bdy">
            <form action="<?php echo base_url();?>index.php/register" method="post">
              <div class="list_rptone">
                <table width="596" style="margin-left:60px; margin-top:20px; margin-bottom:20px;">
                  <tr>
                    <td width="148" class="td_left">First Name*</td>
                    <td width="436" class="td_left"><input type="text" name="f_nmae" value="" style="width:300px;"/>
                    <span class="err"><?php echo form_error('f_nmae'); ?></span></td>
                  </tr>
                  <tr>
                    <td class="td_left">Middle Name</td>
                    <td class="td_left"><input type="text" name="m_nmae" value="" style="width:300px;"/></td>
                  </tr>
                  <tr>
                    <td class="td_left">Last Name*</td>
                    <td class="td_left"><input type="text" name="l_nmae" value="" style="width:300px;"/>
                    <span class="err"><?php echo form_error('l_nmae'); ?></span></td>
                  </tr>
                </table>
              </div>
              <div class="list_rptone">
                <table width="594" style="margin-left:60px; margin-top:20px; margin-bottom:20px;">
                  <tr>
                    <td class="td_left">Company*</td>
                    <td class="td_left"><input type="text" name="company" value="" style="width:300px;" id="zip" />
                    <span class="err"><?php echo form_error('company'); ?></span></td>
                  </tr>
                  <tr>
                    <td width="149" class="td_left">Address*</td>
                    <td width="433" class="td_left"><input type="text" name="Add_one" value="" style="width:300px;"/>
                    <span class="err"><?php echo form_error('Add_one'); ?></span></td>
                  </tr>
                  <tr>
                    <td class="td_left">City, State, Zip</td>
                    <td class="td_left"><input type="text" name="zip" value="" style="width:300px;" id="zip" /></td>
                  </tr>
                </table>
              </div>
              <div class="list_rptone">
                <table width="598" style="margin-left:60px; margin-top:20px; margin-bottom:20px;">
                  <tr>
                    <td width="152" class="td_left">Phone #*</td>
                    <td width="434" class="td_left"><input type="text" name="phn_no" value="" style="width:300px;"/>
                      <span class="err">
                        <?php if(form_error('phn_no')){
                            echo '<p>The phone # field is required</p>';
                            }
                        ?>
                      </span></td>
                  </tr>

                  <tr>
                    <td class="td_left">Email*</td>
                    <td class="td_left"><input type="text" name="Email_id" value="" style="width:300px;"/>
                    <span class="err"><?php echo form_error('Email_id');?></span></td>
                  </tr>
                  <tr>
                    <td class="td_left">Fax #*</td>
                    <td class="td_left"><input type="text" name="fax_no" value="" style="width:300px;"/></td>
                  </tr>
                </table>
              </div>
              <div class="list_rptone">
                <table width="600" style="margin-left:60px; margin-top:20px; margin-bottom:20px;">
                  <tr>
                    <td width="153" class="td_left">User Name*</td>
                    <td width="435" class="td_left"><input type="text" name="user_name" value="" style="width:300px;" />
                      <span class="err"><?php echo form_error('user_name'); ?></span></td>
                  </tr>
                  <tr>
                    <td class="td_left">Password*</td>
                    <td class="td_left"><input type="password" name="Password" value="" style="width:300px;"/>
                      <span class="err"><?php echo form_error('Password'); ?></span></td>
                  </tr>
                  <tr>
                    <td class="td_left">Confirm Password*</td>
                    <td class="td_left"><input type="password" name="confrm_Password" value="" style="width:300px;"/>
                      <span class="err"><?php echo form_error('confrm_Password'); ?></span></td>
                  </tr>
                  <tr>
                    <td colspan="2" align="center" class="td_left">Confirm this Captcha</td>
                  </tr>
                  <tr>
                    <td class="td_left"><?php if ($this->session->flashdata('error') !== FALSE) { echo $this->session->flashdata('error'); } ?></td>
                    <td> <?php echo $recaptcha_html; ?> </td>
                  </tr>
                  <tr>
                    <td class="td_left"></td>
                    <td><input type="submit" value="submit" name="submit" />
                      <a href="<?php echo base_url();?>index.php/register" style="text-decoration:none;">
                      <input type="button" value="cancel" name="cancel"  />
                      </a></td>
                  </tr>
                </table>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
War es hilfreich?

Lösung

It will work just do like this ...

Remove your captacha entries from controllers/register.php : public function __construct()

Just use this in controllers/register.php

public function index()
    {       
     $this->load->library('recaptcha');
     $data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();
     $data['page'] = 'register/content';

Rest you can do.

Andere Tipps

Ok. I guess the problem is You are calling your recaptcha_get_html() function in your constructor, so its scope lies in the constructor, while you are loading your view from index function.

try this-

 public function __construct()
    {
        parent::__construct();
        $this->load->library('recaptcha');
        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->load->library('session');
        $this->load->model('index/user_model');
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
        $data = $this->recaptcha->recaptcha_get_html();
         $this->index($data);    
}

and in your index function write like-

public function index($captcha)
        {       

$data['recaptcha_html']= $captcha;
// rest of your code
......

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top