Solved

Redirecting to a page after reCaptcha is correct!

Posted on 2010-11-10
10
885 Views
Last Modified: 2012-08-13
I have a reCaptcha page I'm adding to my site.  I have incorporated the reCaptcha code, however, I would like to make it so that once the page is verified the user would be sent to a new page.  I thought it would be easy to use the:
header('Location: /x/type.php');
exit;

Open in new window


however, this does not work (it gives a header already sent error).  Can someone point me in the right direction with this one.

Thanks,

jbrich01
<script type = "text/javascript">

    var RecaptchaOptions = {

       theme : 'white'

    };

    </script>

<form action="" method="post">

 <script type = "text/javascript">

    var RecaptchaOptions = {

       theme : 'white'

    };

    </script>

	<?php



require_once('recaptchalib.php');





// Get a key from https://www.google.com/recaptcha/admin/create

$publickey = "OMITTED";

$privatekey = "OMITTED2";



# the response from reCAPTCHA

$resp = null;

# the error code from reCAPTCHA, if any

$error = null;



# was there a reCAPTCHA response?

if ($_POST["recaptcha_response_field"]) {

        $resp = recaptcha_check_answer ($privatekey,

                                        $_SERVER["REMOTE_ADDR"],

                                        $_POST["recaptcha_challenge_field"],

                                        $_POST["recaptcha_response_field"]);



        if ($resp->is_valid) {

			header('Location: /x/type.php');

			exit;

		

        } else {

                # set the error code so that we can display it

                $error = $resp->error;

				

        }

}

echo recaptcha_get_html($publickey, $error);

?>

    <br/>

    <input type="submit" value="submit" />

    </form>

Open in new window

0
Comment
Question by:jbrich01
  • 5
  • 3
  • 2
10 Comments
 
LVL 20

Expert Comment

by:edster9999
ID: 34108197
in your php config file set it to use php buffering and then it will hold back until the headers get sent and that redirection will work
0
 
LVL 20

Accepted Solution

by:
edster9999 earned 250 total points
ID: 34108210
$ locate php.ini

/etc/apache2/php/php.ini

$ vi /etc/apache2/php/php.ini

find the line that says :

output_buffering = off

and set it to on

restart apache and that should re-read that file in php.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34108223
Put the reCaptcha form on the page you want to test.  Put the reCaptcha evaluation on the action script page.  It's just that easy - no special redirects or anything like that.
0
 

Author Comment

by:jbrich01
ID: 34108693
Hi Edster 9999,

I will try that and let you know if it works.  Thanks.
_________________________________________________________________
Hi Ray Paseur,
I tried that, but it did not work for me.  Maybe i put in some extraneous code.  I will give it another try.

Thanks.

jbrich01
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34112817
You might also try adding this line to the top of your scripts:

ob_start();

That will turn on the output buffers.  

But I do not think output buffering is the issue.  I think there is a logic flaw.  Instead of redirecting to /x/type.php, why not just do the processing right there inside the script that just performed the recaptcha verification?  I think that is how the pros would do it.

;-)

~Ray
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:jbrich01
ID: 34161706
Sorry guys, stuck at work for the last few days.  Neither suggestion worked.  Below is the orginal recapture code. The comment line that reads

// Your code here to handle a successful verification

is where I need help.

<?php   require_once('recaptchalib.php');   $privatekey = "your_private_key";   $resp = recaptcha_check_answer ($privatekey,                                 $_SERVER["REMOTE_ADDR"],                                 $_POST["recaptcha_challenge_field"],                                 $_POST["recaptcha_response_field"]);    if (!$resp->is_valid) {     // What happens when the CAPTCHA was entered incorrectly     die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .          "(reCAPTCHA said: " . $resp->error . ")");   } else {     // Your code here to handle a successful verification   }   ?>

Open in new window

0
 

Author Comment

by:jbrich01
ID: 34161711
<?php   require_once('recaptchalib.php');   $privatekey = "your_private_key";   $resp = recaptcha_check_answer ($privatekey,                                 $_SERVER["REMOTE_ADDR"],                                 $_POST["recaptcha_challenge_field"],                                 $_POST["recaptcha_response_field"]);    if (!$resp->is_valid) {     // What happens when the CAPTCHA was entered incorrectly     die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .          "(reCAPTCHA said: " . $resp->error . ")");   } else {     // Your code here to handle a successful verification   }   ?>
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 34164324
Please use the code snippet to post the code - it will be much easier for us to read, and we will have line numbers that facilitate communication.  Thanks.
0
 

Author Comment

by:jbrich01
ID: 34170915
Thanks guys, I solved it.  The problem was a white space issue in the template that I attached to the reCaptra page.
0
 

Author Closing Comment

by:jbrich01
ID: 34170924
The solution turned out to be a white space issue in the template that I was using.  Not the reCaptra code or the redirect header('Location:') line.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now