Page 1 of 1
PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 6:51 am
by bajere
Hi All,
I am after a bit of help with a Captcha spam protection box.
The site gave me instructions, and all is well, apart from if the user give the wrong Captcha information...
The highlighted code is the part where i would like help, i would like the error message to appear in the same area/under submit button, in the form. For example: "*Wrong input for Captcha ", not take the user to a new screen, and make them hit the back button.
ANY help would be greatly appreciated, as im very new to PHP and not really sure whats what.
The code is:
***** PLEASE THE CODE TAG WHEN POSTING SOURCE CODE *****Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "************************";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
[color=#0000FF]if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Please go BACK and try again..." .
"(reCAPTCHA said: " . $resp->error . ")");
}
[/color]
$to = "grant@********";
$subject = "Enquiry";
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$body = "Enquiry From: $name.\n\n$name's Phone Number: $phone.\n\nEnquiry:\n$message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{header( 'Location:******** TEST_ENVIRONMENT/Tatar/thank_you.php' ) ;}
else
{print "We encountered an error sending your enquiry"; }
?>
Thank you in advance
Grant Bajere
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 11:22 am
by markusn00b
There are a couple of ways you could do this:
- If the captcha fails, redirect back to the captcha page with a url parameter indicating the validation failed (see example 1)
- IF the captcha fails, set a variable that indicates the validation failed and load the captcha page (see example 2)
Example 1 - validation_page.php:
Code: Select all
if (!$captcha->run()) {
// Captcha has failed.
// Redirect to page that contains the captcha, with a url parameter.
header("location: captcha.php?failed=1");
}
// Send email
Example 1 - captcha.php:
Code: Select all
// You may put this 'neath the captcha image, or wherever takes your fancy.
if (isset($_GET['failed']) && $_GET['failed'] == '1') {
// The validation has redirected back with the failure signal
echo 'The validation failed.';
}
Example 2 - validation_page.php:
Code: Select all
if (!$captcha->run()) {
// Captcha failed
// Load the captcha
define('CAPTCHA_FAILURE', 1);
include 'captcha.php';
}
// Note the use of the else here.
else {
// mail
}
Example 2 - captcha.php:
Code: Select all
// put this 'neath your captcha, or wherever takes your fancy
if (defined('CAPTCHA_FAILURE') && CAPTCHA_FAILURE === 1) {
// failure constant was set
echo 'Validation failed.';
}
Hope this helps,
Mark.
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 11:42 am
by bajere
thank you for your fast reply !
ill have a look at that now and see how i get on, thanks for the labeling also, helps lots as im not up to speed on php.
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 12:18 pm
by markusn00b
bajere wrote:thank you for your fast reply !
ill have a look at that now and see how i get on, thanks for the labeling also, helps lots as im not up to speed on php.
Don't sweat it.
Let us know of any problems you have!
Mark.
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 12:43 pm
by bajere
I have had a go. I couldn't get any joy with either of them? but i did get it working for the
"Thank You" part, which is another thing i was after and thought they would be the same. but the try again code isn't having it. i think because your code dosen't have: if (!$resp->is_valid). i have tried a few different combo's, but still nothing
This is what i have so far (red stuff is the original code, that i deleted):
***** PLEASE THE CODE TAG WHEN POSTING SOURCE CODE *****Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "***************";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
[color=#FF0000]if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Please go BACK and try again..." .
"(reCAPTCHA said: " . $resp->error . ")");
}[/color]
if (!$captcha->run()) {
// Captcha has failed.
// Redirect to page that contains the captcha, with a url parameter.
header("location: captcha.php?failed=1");
}
// Send email
$to = "grant@**********l.co.uk";
$subject = "Enquiry";
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$body = "Enquiry From: $name.\n\n$name's Phone Number: $phone.\n\nEnquiry:\n$message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{header( 'Location: contact.php?worked=1' ) ;}
else
{print "We encountered an error sending your enquiry"; }
?>
___________
Thanks again
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 12:46 pm
by markusn00b
Hehe.
I should've mentioned the $captcha->run() was simply pseudo-code. You need to change this to whatever it was ($resp->is_valid) or something.

Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 1:09 pm
by bajere
ah right.lol.
I did try it with the ($resp->is_valid) as well but still no joy. it just ignores everything and says its valid now?? has this part got anything to do with it do you think?
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Please go BACK and try again..." .
"(reCAPTCHA said: " . $resp->error . ")");
}
its really odd, as it works for the other side (the thank you part)...
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 1:38 pm
by markusn00b
bajere wrote:ah right.lol.
I did try it with the ($resp->is_valid) as well but still no joy. it just ignores everything and says its valid now?? has this part got anything to do with it do you think?
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Please go BACK and try again..." .
"(reCAPTCHA said: " . $resp->error . ")");
}
its really odd, as it works for the other side (the thank you part)...
Can I see your code as it is now?
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 1:55 pm
by bajere
sure, its:
PROCESS:
***** PLEASE THE CODE TAG WHEN POSTING SOURCE CODE *****Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "*******************************";
$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. Please go BACK and try again..." .
"(reCAPTCHA said: " . $resp->error . ")");
}
$to = "grant@**********.uk";
$subject = "Enquiry";
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$body = "Enquiry From: $name.\n\n$name's Phone Number: $phone.\n\nEnquiry:\n$message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{header( 'Location: contact.php?worked=1' ) ;}
else
{print "We encountered an error sending your enquiry"; }
?>
FORM(section) :
<div id="left">
<?php
// You may put this 'neath the captcha image, or wherever takes your fancy.
if (isset($_GET['worked']) && $_GET['worked'] == '1') {
// The validation has redirected back with the failure signal
echo '<p class="big_bold_text_green">Thank You, your Enquiry<br> Has Been Sent.</p>';
}
?>
<?php
// You may put this 'neath the captcha image, or wherever takes your fancy.
if (isset($_GET['failed']) && $_GET['failed'] == '1') {
// The validation has redirected back with the failure signal
echo 'The validation failed.';
}
?>
<form action="form_submit.php" method="post">
<p class="big_bold_text_green">Contact form</p>
<p class="bold_text_green">Please complete and submit the form below</p>
<label><span class="form_text">Name:</span><input name="name" type="text" class="input_box" tabindex="1" /></label>
<label><span class="form_text">Phone:</span><input name="phone" type="text" class="input_box" tabindex="2" id="phone" /></label>
<label><span class="form_text">Email:</span><input name="email" type="text" class="input_box" tabindex="3" id="email" /></label>
<label><span class="form_text">Message:</span><textarea name="message" class="input_box_multi" tabindex="4" id="email"></textarea></label>
<div class="recap">
<?php
require_once('recaptchalib.php');
$publickey = "6Ld38QgAAAAAAEHbgE0pLyRDOqpLIAULdu6xnoyq";
echo recaptcha_get_html($publickey);
?>
</div>
<lable><span class="submit_note">Please note all fields are required.</span><input type="image" class="submit" src="images/submit.jpg" alt="Submit Form" border="0"></label>
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=<6Ld38QgAAAAAAEHbgE0pLyRDOqpLIAULdu6xnoyq>">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=<6Ld38QgAAAAAAEHbgE0pLyRDOqpLIAULdu6xnoyq>"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
</form>
</div>
Re: PHP Captcha Error help - replace the "die" command
Posted: Wed Oct 21, 2009 3:17 pm
by markusn00b
Please use code tags when posting code, as per the
forum guidelines.
Your process page should look like:
Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "*******************************";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
header("location: form.php?failed=1");
exit;
}
$to = "grant@**********.uk";
$subject = "Enquiry";
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$body = "Enquiry From: $name.\n\n$name's Phone Number: $phone.\n\nEnquiry:\n$message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{header( 'Location: contact.php?worked=1' ) ;}
else
{print "We encountered an error sending your enquiry"; }
?>
Note: you'll have to change the location that the header points to, to reflect your form page.
Mark.
Re: PHP Captcha Error help - replace the "die" command
Posted: Thu Oct 22, 2009 5:00 am
by bajere
THANKS!!
It was the exit; part that was missing. works a treat!
Not sure if you do know as i think its a Java Script piece of programming, but is there away of making sure the form stays filled in? so that if they do end up failing, the form is ready to be resubmitted...
Thanks again, i have another project that needs this, so you have helped me double

Re: PHP Captcha Error help - replace the "die" command
Posted: Thu Oct 22, 2009 7:27 am
by markusn00b
bajere wrote:THANKS!!
It was the exit; part that was missing. works a treat!
Not sure if you do know as i think its a Java Script piece of programming, but is there away of making sure the form stays filled in? so that if they do end up failing, the form is ready to be resubmitted...
Thanks again, i have another project that needs this, so you have helped me double

Sure thing, Bajere. Don't sweat it - we're here to help
You want to the form to retain its values, you say? No problem. No problem at all.
Let's diagnose the problem: when you submit a form (assuming the form's method = POST), you are then able to obtain those values via the $_POST array. If we were submitting the form to the same page, we could simply echo these values ($_POST['form_el_name']) into the form elements' "value" attribute, like below:
Code: Select all
<?php
function form_val($el, $arr, $def = '')
{
return (isset($arr[$el]) ? $arr[$el] : $def);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="test" value="<?php print form_val('test', $_POST, 'default val');?>" />
<input type="submit" name="submit" />
</form>
However, when you are calling the header() function, this information is lost. How can we preserve the information? Simple. Just as we are doing with the 'success' or 'failure' URL parameters, we'll create the form parameters for the URL also. Note: you'll have to change the $_POST indexes to reflect the data submitted by the form:
Code: Select all
if (!$resp->is_valid) {
// Remember to changes these to what you want!
$username = $_POST['username'];
$email = $_POST['email'];
header("location: form.php?failed=1&user=$username&email=$email");
exit;
}
You can then check for these values in your form:
Code: Select all
<?php
function form_val($el, $arr, $def = '')
{
return (isset($arr[$el]) ? $arr[$el] : $def);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="username" value="<?php print form_val('username', $_GET);?>" />
<input type="text" name="email" value="<?php print form_val('email', $_GET);?>" />
<input type="submit" name="submit" />
</form>
Let me know if you need anymore help.
Thanks,
Mark.
Re: PHP Captcha Error help - replace the "die" command
Posted: Thu Oct 22, 2009 8:38 am
by bajere
I have that bit working now, but the captca wont work properly??
If i leave the exit; in, it will always say "incorrect captca", if i take exit; out it awlays says "thank you".
it must only be something slightly wrong.lol
Code: Select all
if (!$resp->is_valid) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
header("location: contact.php?failed=1&name=$name&phone=$phone&email=$email&message=$message");
exit;
}
and form:
Code: Select all
<div id="left">
<?php
function form_val($el, $arr, $def = '')
{
return (isset($arr[$el]) ? $arr[$el] : $def);
}
?>
<form action="form_submit.php" method="post">
<p class="big_bold_text_green">Contact form</p>
<p class="bold_text_green">Please complete and submit the form below</p>
<label><span class="form_text">Name:</span><input name="name" type="text" class="input_box" tabindex="1" id="name" value="<?php print form_val('name', $_GET);?>" /></label>
<label><span class="form_text">Phone:</span><input name="phone" type="text" class="input_box" tabindex="2" id="phone" value="<?php print form_val('phone', $_GET);?>" /></label>
<label><span class="form_text">Email:</span><input name="email" type="text" class="input_box" tabindex="3" id="email" value="<?php print form_val('email', $_GET);?>" /></label>
<label><span class="form_text">Message:</span><textarea name="message" class="input_box_multi" tabindex="4" id="message" /><?php print form_val('message', $_GET);?></textarea></label>
not really sure why it dosen't work...