代码之家  ›  专栏  ›  技术社区  ›  Malachi majid hussain

recaptcha验证不会与codeigniter一起抛出-但仅当上载到webhost时

  •  2
  • Malachi majid hussain  · 技术社区  · 14 年前

    使用以下联机指南(以及底部的1.7x修复程序) http://codeigniter.com/forums/viewthread/94299/

    Recaptcha验证在本地工作时(在我的windows计算机和朋友的OSX计算机上都进行了测试)效果很好,但是在上载我的服务器和他的web主机时,即使页面上的其他验证也不会检查recaptch的验证例程。但是没有抛出任何错误,我已经查看了代码,无法找出出了什么问题。如果有人能帮我弄清楚发生了什么,我将不胜感激。

    以下是处理这个问题的类

    应用程序/库/MY_Form_Validation.php

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class MY_Form_Validation extends CI_Form_Validation
    {
    
        function MY_Form_Validation()
        {
            parent::CI_Form_Validation();
        }
    
        function recaptcha_matches() {
            $CI = & get_instance();
            $CI->config->load('recaptcha');
            $public_key = $CI->config->item('recaptcha_public_key');
            $private_key = $CI->config->item('recaptcha_private_key');
            $response_field = $CI->input->post('recaptcha_response_field');
            $challenge_field = $CI->input->post('recaptcha_challenge_field');
            $response = recaptcha_check_answer($private_key, $_SERVER['REMOTE_ADDR'], $challenge_field, $response_field);
    
            if ($response->is_valid) {
                return TRUE;
            }
            else {
                $this->recaptcha_error = $response->error;
                $this->set_message('recaptcha_matches', 'The %s is incorrect. Please try again.');
                return FALSE;
            }
        }
    

    应用程序/helpers/recaptcha_helper.php

    http://pastebin.com/m352494c3

    application/controllers/forum.php-后线程方法

    public function add_thread($category_id = null)
    {
        $category = $this->forums->get_category($category_id);
        if ($category == false)
        {
            $this->template->set('title', 'Invalid Page');
            $data['error_message'] = "Invalid category to add thread too";
            $this->template->load('template', 'error', $data);
            return;
        }
    
        $user = $this->users->get($this->session->userdata('user_id'));
    
        $data['category_id'] = $category['id'];
        $data['category_title'] = $category['category_title'];
        $data['loggedin'] = $this->session->userdata('loggedin');
    
        $this->form_validation->set_rules('thread_title', 'Title', 'required');
        $this->form_validation->set_rules('thread_body', 'Body', 'required');
    
        if (!$data['loggedin'])
        {
            $this->form_validation->set_rules('recaptcha_challenge_field', 'answer to the security question', 'recaptcha_matches');
        }
    
        if ($this->form_validation->run() == FALSE)
        {
            $this->template->load('template', 'forums/add_thread', $data);
            return;
        }
    
        $thread['thread_title'] = $this->input->post('thread_title');
        $thread['thread_body'] = $this->input->post('thread_body');
        if($user==false){
            $thread['user_id'] = -1;
        }else{
            $thread['user_id'] = $user->id;
        }
        $thread['posted_date'] = date("Y-m-d H:i:s");
        $thread['category_id'] = $category_id;
    
        $this->forums->add_thread($thread);
        redirect('forums/view/' . $category_id, 'refresh');
    }
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Donny Kurnia    14 年前

    在我的代码中,我通常是这样写的:

    $response = recaptcha_check_answer($private_key, $CI->input->ip_address(), $challenge_field, $response_field);
    

    $CI->input->ip_address() 有更复杂的方法来检测客户端IP地址。比使用 REMOTE_ADDR 一个人。

    另外,请确保您的私钥和公钥与服务器的域名匹配,或者使用全局密钥。