Solved

Redirecting to a page after reCaptcha is correct!

Posted on 2010-11-10
10
923 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 109

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

803 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