我在codeigniter中输入无效的验证码时看不到错误消息

时间:2018-06-24 18:59:24

标签: php codeigniter captcha

我使用Google验证码。我已经通过以下链接在CodeIgniter项目中进行了安装: https://github.com/mehdibo/Codeigniter-recaptcha 问题是当我不输入验证码时,我看不到错误消息。 和我的数据输入成功!但是不必输入数据,因为验证码错误。 “对不起我的英语不好”

我整个受控的register.php

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

class Register extends CI_Controller 
{

    public function __construct() 
    {
        parent::__construct();
        $this->load->model("register_model");
        $this->load->model("user_model");
    }

    public function index()
    {


            //begin captcha


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

        /*
         Create the reCAPTCHA box.
         You can pass an array of attributes to this method.
         Check the "Creating the reCAPTCHA box" section for more details
        */
        $recaptcha = $this->recaptcha->create_box();

        // Check if the form is submitted
        if($this->input->post('action') === 'submit')
        {
            /*
             Check if the reCAPTCHA was solved
             You can pass arguments to the `is_valid` method,
             but it should work fine without any.
             Check the "Validating the reCAPTCHA" section for more details
            */
            $is_valid = $this->recaptcha->is_valid();

            if($is_valid['success'])
            {
                echo "reCAPTCHA solved";
            }
            else
            {
                echo "reCAPTCHA not solved/an error occured";
            }
        }


            //end off recaptcha


            //begin of upload script
            //Check whether user upload picture
            if(!empty($_FILES['picture']['name'])){
           $config['upload_path'] = FCPATH .'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['encrypt_name'] = TRUE;
$config['overwrite'] = FALSE;
$config['max_filename'] = 300;
$config['remove_spaces'] = TRUE;
$config['max_size'] = 2048;



                //Load upload library and initialize configuration
                $this->load->library('upload',$config);


                if($this->upload->do_upload('picture')){
   $picture = $this->upload->data('file_name');                    
                }else{
                    $picture = '';
                }
            }else{
                $picture = '';
            }

//end of upload script


        if ($this->user_model->check_block_ip()) {
            $this->template->error(lang("error_26"));
        }

        $this->template->set_error_view("error/login_error.php");
        $this->template->set_layout("layout/login_layout.php");
        if ($this->settings->info->register) {
            $this->template->error(lang("error_54"));
        }

        $this->template->loadExternal(
            '<script type="text/javascript" src="'
            .base_url().'scripts/custom/check_username.js" /></script>'
        );

        if ($this->user->loggedin) {
            $this->template->error(
                lang("error_27")
            );
        }
        $this->load->helper('email');

        $email = "";
        $name = "";
        $username = "";
        $fail = "";
        $first_name = "";
        $last_name = "";
        $phone = "";
        $city = "";
        $address = "";
        $jobs = "";
        $education = "";
        $diplome = "";
        $worktime = "";
        $freeservices = "";


        if (isset($_POST['s'])) {
            $email = $this->input->post("email", true);
            $first_name = $this->common->nohtml(
                $this->input->post("first_name", true));
            $last_name = $this->common->nohtml(
                $this->input->post("last_name", true));
                $phone = $this->common->nohtml(
                $this->input->post("phone", true));
                $city = $this->common->nohtml(
                $this->input->post("city", true));
                $address = $this->common->nohtml(
                $this->input->post("address", true));
                $jobs = $this->common->nohtml(
                $this->input->post("jobs", true));
                $education = $this->common->nohtml(
                $this->input->post("education", true));
                $diplome = $this->common->nohtml(
                $this->input->post("diplome", true));
                $worktime = $this->common->nohtml(
                $this->input->post("worktime", true));
                $freeservices = $this->common->nohtml(
                $this->input->post("freeservices", true));
            $pass = $this->common->nohtml(
                $this->input->post("password", true));
            $pass2 = $this->common->nohtml(
                $this->input->post("password2", true));
            $captcha = $this->input->post("captcha", true);
            $username = $this->common->nohtml(
                $this->input->post("username", true));

            if (strlen($username) < 3) $fail = "error_31";

            if (!preg_match("/^[a-z0-9_]+$/i", $username)) {
                $fail = lang("error_15");
            }

            if (!$this->register_model->check_username_is_free($username)) {
                $fail = lang("error_16");
            }

            if (!$this->settings->info->disable_captcha) {
                if ($captcha != $_SESSION['sc']) {
                    $fail = lang("error_55");
                }
            }
            if ($pass != $pass2) $fail = lang("error_22");

            if (strlen($pass) <= 5) {
                $fail = lang("error_17");
            }

            if (strlen($first_name) > 25) {
                $fail = lang("error_56");
            }
            if (strlen($last_name) > 30) {
                $fail = lang("error_57");
            }
if (strlen($phone) > 40) {
                $fail = lang("error_57");
            }
            if (strlen($city) > 40) {
                $fail = lang("error_57");
            }
            if (strlen($address) > 400) {
                $fail = lang("error_57");
            }
            if (strlen($jobs) > 80) {
                $fail = lang("error_57");
            }
            if (strlen($education) > 500) {
                $fail = lang("error_57");
            }
            if (strlen($diplome) > 500) {
                $fail = lang("error_57");
            }
            if (strlen($worktime) > 500) {
                $fail = lang("error_57");
            }
            if (strlen($freeservices) > 50) {
                $fail = lang("error_57");
            }
            if (empty($first_name) || empty($last_name)) {
                $fail = lang("error_58");
            }

            if (empty($email)) {
                $fail = lang("error_18");
            }

            if (!valid_email($email)) {
                $fail = lang("error_19");
            }

            if (!$this->register_model->checkEmailIsFree($email)) {
                $fail = lang("error_20");
            }

            if (empty($fail)) {

                $pass = $this->common->encrypt($pass);
                $active = 1;
                $activate_code = "";
                $success =  lang("success_20");
                if($this->settings->info->activate_account) {
                    $active = 0;
                    $activate_code = md5(rand(1,10000000000) . "fhsf" . rand(1,100000));
                    $success = lang("success_33");

                    // Send email
                    $this->load->model("home_model");
                    $email_template = $this->home_model->get_email_template(2);
                    if($email_template->num_rows() == 0) {
                        $this->template->error(lang("error_48"));
                    }
                    $email_template = $email_template->row();

                    $email_template->message = $this->common->replace_keywords(array(
                        "[NAME]" => $username,
                        "[SITE_URL]" => site_url(),
                        "[EMAIL_LINK]" => 
                            site_url("register/activate_account/" . $activate_code . 
                                "/" . $username),
                        "[SITE_NAME]" =>  $this->settings->info->site_name
                        ),
                    $email_template->message);

                    $this->common->send_email($email_template->title,
                         $email_template->message, $email);
                }

                $userid = $this->register_model->add_user(array(
                    "username" => $username,
                    "email" => $email,
                    "first_name" => $first_name,
                    "last_name" => $last_name,
                    "phone" => $phone,
                    "city" => $city,
                    "address" => $address,
                    "jobs" => $jobs,
                    "education" => $education,
                    "diplome" => $diplome,
                    "worktime" => $worktime,
                    "freeservices" => $freeservices,
                    "password" => $pass,
                    'picture' => $picture,
                    "user_role" => 0,
                    "IP" => $_SERVER['REMOTE_ADDR'],
                    "joined" => time(),
                    "joined_date" => date("n-Y"),
                    "active" => $active,
                    "activate_code" => $activate_code

                    )
                );

                // Check for any default user groups
                $default_groups = $this->user_model->get_default_groups();
                foreach($default_groups->result() as $r) {
                    $this->user_model->add_user_to_group($userid, $r->ID);
                }
                $this->template->set_error_view("succ/regi_succ.php");

                $this->template->error(lang("success_20"));

            }

        }


        $this->load->helper("captcha");
        $rand = rand(4000,100000);
        $_SESSION['sc'] = $rand;
        $vals = array(
            'word' => $rand,
            'img_path' => './images/captcha/',
            'img_url' => base_url() . 'images/captcha/',
            'img_width' => 150,
            'img_height' => 30,
            'expiration' => 7200
            );

        $cap = create_captcha($vals);
        $data = array(
            "cap" => $cap,
            "email" => $email,
            "first_name" => $first_name,
            "last_name" => $last_name,
            "phone" => $phone,
            "city" => $city,
            "address" => $address,
            "jobs" => $jobs,
            "education" => $education,
            "diplome" => $diplome,
            "worktime" => $worktime,
            "freeservices" => $freeservices,
            'picture' => $picture,
            'fail' => $fail,
            "username" => $username,
           'recaptcha' => $recaptcha
        );
        $this->template->loadContent("register/index.php", $data);

    }

    public function add_username() 
    {
        $this->template->loadExternal(
            '<script type="text/javascript" src="'
            .base_url().'scripts/custom/check_username.js" /></script>'
        );
        if (!$this->user->loggedin) {
            $this->template->error(
                lang("error_1")
            );
        }
        $this->template->loadContent("register/add_username.php", array());
    }

    public function add_username_pro() 
    {
        $this->load->helper('email');
        $email = $this->input->post("email", true);
        $username = $this->common->nohtml(
                $this->input->post("username", true));
        if (strlen($username) < 3) $fail = lang("error_14");

        if (!preg_match("/^[a-z0-9_]+$/i", $username)) {
            $fail = lang("error_15");
        }

        if (!$this->register_model->check_username_is_free($username)) {
            $fail = lang("error_16");
        }
        if (empty($email)) {
            $fail = lang("error_18");
        }

        if (!valid_email($email)) {
            $fail = lang("error_19");
        }

        if (!$this->register_model->checkEmailIsFree($email)) {
            $fail = lang("error_20");
        }

        if(!empty($fail)) $this->template->error($fail);

        $this->register_model
            ->update_username($this->user->info->ID, $username, $email);
        $this->session->set_flashdata("globalmsg",  lang("success_21"));
        redirect(site_url());
    }

    public function check_username() 
    {
        $username = $this->common->nohtml(
                $this->input->get("username", true));
        if (strlen($username) < 3) $fail = lang("error_14");

        if (!preg_match("/^[a-z0-9_]+$/i", $username)) $fail = lang("error_15");

        if (!$this->register_model->check_username_is_free($username)) {
            $fail="$username " . lang("ctn_243");
        }
        if (empty($fail)) {
            echo"<span style='color:#4ea117'>". lang("ctn_244")."</span>";
        } else {
            echo $fail;
        }
        exit();
    }

    public function activate_account($code, $username) 
    {
        $code = $this->common->nohtml($code);
        $username = $this->common->nohtml($username);

        $code = $this->user_model->get_verify_user($code, $username);
        if($code->num_rows() == 0) {
            $this->template->error(lang("error_69"));
        }
        $code = $code->row();
        if($code->active) {
            $this->template->error(lang("error_69"));
        }

        $this->user_model->update_user($code->ID, array(
            "active" => 1, 
            "activate_code" => ""
            )
        );

        $this->session->set_flashdata("globalmsg", 
            lang("success_34"));
        redirect(site_url("login"));
    }

    public function send_activation_code($userid, $email) 
    {
        $userid = intval($userid);
        $email = $this->common->nohtml(urldecode($email));

        // Check request
        $request = $this->user_model->get_user_event("email_activation_request");
        if($request->num_rows() > 0) {
            $request = $request->row();
            if($request->timestamp + (15*60) > time()) {
                $this->template->error(lang("error_70"));
            }
        }

        $this->user_model->add_user_event(array(
            "event" => "email_activation_request",
            "IP" => $_SERVER['REMOTE_ADDR'],
            "timestamp" => time()
            )
        );

        // Resend
        $user = $this->user_model->get_user_by_id($userid);
        if($user->num_rows() == 0) {
            $this->template->error(lang("error_71"));
        }
        $user = $user->row();
        if($user->email != $email) 
        {
            $this->template->error(lang("error_71"));
        }
        if($user->active) {
            $this->template->error(lang("error_71"));
        }
        // Send email
        $this->load->model("home_model");
        $email_template = $this->home_model->get_email_template(2);
        if($email_template->num_rows() == 0) {
            $this->template->error(lang("error_48"));
        }
        $email_template = $email_template->row();

        $email_template->message = $this->common->replace_keywords(array(
            "[NAME]" => $user->username,
            "[SITE_URL]" => site_url(),
            "[EMAIL_LINK]" => 
                site_url("register/activate_account/" . $user->activate_code . 
                    "/" . $user->username),
            "[SITE_NAME]" =>  $this->settings->info->site_name
            ),
        $email_template->message);

        $this->common->send_email($email_template->title,
             $email_template->message, $user->email);
        $this->session->set_flashdata("globalmsg", 
            lang("success_35"));
        redirect(site_url("login"));
    }




    }

?>

我的观点的一部分:

<div class="col-xs-12 col-sm-6 col-md-6">
                        <?=$recaptcha?>
                    </div>

1 个答案:

答案 0 :(得分:0)

我的“准”答案:

if($this->input->post('action') === 'submit')中的

(1)在顶部exit('something')处添加...它表示什么?如果是,则(2)var_dump($is_valid); exit; ...结果是否符合预期?如果是,则(3)认为重定向可能会阻止显示回声。将来,如果这些方法都不能帮助您找到问题,请缩小,将代码缩小到可管理的位,以解决问题所在。