PHP Captcha with cookies

428 views
Skip to first unread message

ZackR

unread,
Jan 12, 2012, 4:08:05 PM1/12/12
to reCAPTCHA
I am trying to create a chaptcha that a user only has to submit once
so they can browse a website without having to complete a captcha any
time they want to fill out a form. I plan to accomplish this by the
use of a setcookie() that will expire in a year. However, when I try
to verify that I am a human, it gives me this error message:

Warning: Cannot modify header information - headers already sent by
(output started at /home/rusawn5/public_html/arrowheaddev/inc/
template.php:12) in /home/rusawn5/public_html/arrowheaddev/
verifyHuman.php on line 23

Now, I have commented out the setcookie() and it works fine so I know
my problem lies with the cookie. I would appreciate any help with a
work-around.

Here is the website: http://dev.arrowheadcfo.com/

Here is the Captcha: http://dev.arrowheadcfo.com/verify.php

Here is the Code of verifyHuman.php:

<?php
require_once('inc/recaptchalib.php');
$privatekey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it
again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {

$camperExpire = time()+60*60*24*30*12;
setcookie("cfoCamper", "human", $camperExpire);
echo "<br><br><center><h3>Thank you very much for verifying that
you are a CFO camper! You may now browse the site without having to be
bothered with the captcha again.</h3></center><br><br>";
}?>

PJH

unread,
Jan 12, 2012, 4:20:08 PM1/12/12
to reca...@googlegroups.com
On Thu, Jan 12, 2012 at 9:08 PM, ZackR <rusaw...@gmail.com> wrote:
Warning: Cannot modify header information - headers already sent by
(output started at /home/rusawn5/public_html/arrowheaddev/inc/
template.php:12) in /home/rusawn5/public_html/arrowheaddev/
verifyHuman.php on line 23

Have you actually *read* the error message?

# Cannot modify header information - headers already sent
# output started at /home/rusawn5/public_html/arrowheaddev/inc/template.php:12

This is not a reCAPTCHA problem.
 
 [...]
 
Here is the Code of verifyHuman.php:

<?php
require_once('inc/recaptchalib.php');
 $privatekey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
 $resp = recaptcha_check_answer ($privatekey,
                               $_SERVER["REMOTE_ADDR"],
                               $_POST["recaptcha_challenge_field"],
                               $_POST["recaptcha_response_field"]);

 if (!$resp->is_valid) {
   die ("The reCAPTCHA wasn't entered correctly. Go back and try it
again." .
        "(reCAPTCHA said: " . $resp->error . ")");
 } else {

         $camperExpire = time()+60*60*24*30*12;
         setcookie("cfoCamper", "human", $camperExpire);
         echo "<br><br><center><h3>Thank you very much for verifying that
you are a CFO camper! You may now browse the site without having to be
bothered with the captcha again.</h3></center><br><br>";
 }?>

That either isn't the file you had when you got that error, or it's not the whole file. The error message indicates something happening on line 23 opf this file. After sorting out the line-breaks introduced by your email client I reckon there's only 17 there.

--
PJH


ZackR

unread,
Jan 12, 2012, 5:17:05 PM1/12/12
to reCAPTCHA
My Bad,

I edited out some of the code in the beginning that includes the
template as well as establishes a connection with the database.

Line 23 is as follows: setcookie("cfoCamper", "human",
$camperExpire);

I also know that this isn't a reCaptcha problem but I figure that the
captcha is affecting it somehow so I posted the issue here.

Thanks again for your help!

On Jan 12, 1:20 pm, PJH <pauljherr...@gmail.com> wrote:

PJH

unread,
Jan 12, 2012, 5:26:40 PM1/12/12
to reca...@googlegroups.com


On Thu, Jan 12, 2012 at 10:17 PM, ZackR <rusaw...@gmail.com> wrote:
Line 23 is as follows: setcookie("cfoCamper", "human",
$camperExpire);

This causes an extra header line to be emitted. Since your templates seem to be sending output before this point, no more headers can be set here.

The warning message is simply telling you, effectively, that this line is (currently) a no-op and your cookies won't be set. You either need to set the cookie before the template is called, or stop your template sending any output.

--
PJH


ZackR

unread,
Jan 13, 2012, 5:08:28 PM1/13/12
to reCAPTCHA
I took out my includes for my template and db connection and I still
get the same error and the problem still lies with the line that has
the setcookie() function. The error is as follows:

Warning: Cannot modify header information - headers already sent by
(output started at /home/rusawn5/public_html/arrowheaddev/inc/
template.php:12) in /home/rusawn5/public_html/arrowheaddev/
verifyHuman.php on line 23

Any Ideas at all? The Code is Below

Zack


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Thank you!</title>
</head>

<body>
<?php
require_once('inc/recaptchalib.php');
$privatekey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it
again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {

$camperExpire = time()+60*60*24*30*12;
setcookie("cfoCamper", "human", $camperExpire);
echo "<br><br><center><h3>Thank you very much for verifying that
you are a CFO camper! You may now browse the site without having to be
bothered with the captcha again.</h3></center><br><br>";
}?>
</body>
</html>

Pep Sanchez

unread,
Jan 13, 2012, 6:13:04 PM1/13/12
to reca...@googlegroups.com
Hey test put ../inc/ no inc/ put "../inc/ bufff. repeat.-

2012/1/13 ZackR <rusaw...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "reCAPTCHA" group.
To post to this group, send email to reca...@googlegroups.com.
To unsubscribe from this group, send email to recaptcha+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/recaptcha?hl=en.


Dominik Sekotill

unread,
Jan 13, 2012, 6:47:01 PM1/13/12
to reca...@googlegroups.com
You can't send headers after you've sent any part of the output. You should check the captcha and send the cookie before any HTML

Pep Sanchez

unread,
Jan 14, 2012, 9:40:50 AM1/14/12
to reca...@googlegroups.com
if you test first the cookie if exist not print the captcha?

2012/1/14 Dominik Sekotill <ko...@gmx.co.uk>
Reply all
Reply to author
Forward
0 new messages