Solved

Redirecting to a page after reCaptcha is correct!

Posted on 2010-11-10
10
998 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 110

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
 

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 77
Windows 10, 64 bit WAMP - won't start 9 29
Google Recaptcha 13 52
replacate wordpress websites 3 23
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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