English 中文(简体)
recaptcha validation not being thrown with codeigniter - however only when uploaded to webhost
原标题:

Using the following guide online (and the 1.7x fix at the bottom) http://codeigniter.com/forums/viewthread/94299/

Recaptcha validation works well when working locally (tested on both my windows machine and a friends OSX machine) however when uploaded too both my server and his web host the validation routine for the recaptch appears not get checked even though the other validation on the pages do. However no errors are thrown and I have looked through the code and unable to find out what is going wrong. If anyone could help me find out what is going on it would be greatly appreciated.

here are the classes that deal with this

application/libraries/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;
        }
    }

application/helpers/recaptcha_helper.php

http://pastebin.com/m352494c3

application/controllers/forum.php - post thread method

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 );
}
问题回答

In my code, I usually write it like this:

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

$CI->input->ip_address() have more sophisticated way to detect client IP Address. than using REMOTE_ADDR alone.

Also, make sure that you have both private and public key matched the server s domain name, or use global key.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签