Jump to content

reCaptcha implementation not working


kuran

Recommended Posts

I have been following these instructions: http://code.google.com/apis/recaptcha/docs/php.htmlI currently have this form on my website:

<form action="verify.php" method="post"><fieldset class="two-col"> <p class="left"> <br/>Full name<span class="required"> *</span><br/> <input type="text" name="fullname" class="texta"/> <br/>Telephone number<br/><input type="text" name="tel" class="texta"/><br/><br/>Your message<span class="required"> *</span><br/><br/><textarea name="comments" rows="8" cols="1" class="textb"></textarea></p> <p class="right"> <br/>Email address<span class="required"> *</span><br/><br/> <input type="text" name="email" class="texta"/></p> <?php		  require_once('recaptchalib.php');		  $publickey = "xxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page		  echo recaptcha_get_html($publickey);		?></fieldset> <div class="submitbutton1"><br/><input value="Submit" type="image" src="images/submitbutton.png" width="102" height="43" ></p><br/><span class="required">*</span> indicates a required field.<br/>All Requests for quotations are confidential.</form>

I have already implemented the reCaptcha information on this page.This form 'posts' in verify.php, and that is where I fail to include the required reCaptcha code. I just can't seem to integrate it. This is verify.php without the reCaptcha code:

<?php/* Set e-mail recipient */$myemail  = "xxxxxxxxxxxxx";/* Check all form inputs using check_input function */$fullname = check_input($_POST['fullname'], "Please enter your name.");$tel  = check_input($_POST['tel']);$email	= check_input($_POST['email']);$comments = check_input($_POST['comments'], "Please write your comments.");/* If e-mail is not valid show error message */if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){	show_error("It seems your E-mail address is not valid.");}/* Let's prepare the message for the e-mail */$message = "Hello!Your contact form has been submitted by:Name: $fullnameE-mail: $emailTel: $telComments:$commentsEnd of message";/* Send the message using mail() function */mail($myemail, $subject, $message);/* Redirect visitor to the thank you page */header('Location: thanks.php');exit();/* Functions we used */function check_input($data, $problem=''){	$data = trim($data);	$data = stripslashes($data);	$data = htmlspecialchars($data);	if ($problem && strlen($data) == 0)	{		show_error($problem);	}	return $data;}function show_error($myError){?>	<?php echo $myError; ?><?phpexit();}?>

This is the bit of code that I need to splice into my content.php file: The guide tells me that 'The following code should be placed at the top of the verify.php file:', however in the case of my mail form it does not seem to be that simple. Unfortunately the reCaptcha website does not have a lot of documentation.. I hope someone would be nice enough to help me. Thanks in advance!

 <?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  }  ?>

Link to comment
Share on other sites

Why don't you stick all your verify.php code in between the 'else' brackets where it says "// Your code here to handle a successful verification" ?Also

  $privatekey = "your_private_key";

wants you to insert the key you got from the signup

Link to comment
Share on other sites

Why don't you stick all your verify.php code in between the 'else' brackets where it says "// Your code here to handle a successful verification" ?Also
  $privatekey = "your_private_key";

wants you to insert the key you got from the signup

Hi Chokk, that is exactly where it is going wrong, let me show you the code and the subsequent error message on the bottom of this email.In my version I included the private and public keys, I should have been clear about that in my initial post.This is one of the errors I get, line 181 is the final line in the verify.php document.. so I am doing something wrong with just pasting my code in.
Parse error: syntax error, unexpected $end in /home/content/13/6882013/html/contactphp.php on line 181
Here is how it looks if I paste my existing verify.php contents into the reCaptcha line:
<?php  require_once('recaptchalib.php');  $privatekey = "xxxxxxxxxxxxxxxxxxxxxxx";  $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 {	/* Set e-mail recipient */$myemail  = "info@test.com";/* Check all form inputs using check_input function */$fullname = check_input($_POST['fullname'], "Please enter your name.");$tel  = check_input($_POST['tel']);$email	= check_input($_POST['email']);$comments = check_input($_POST['comments'], "Please write your comments.");/* If e-mail is not valid show error message */if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){	show_error("It seems your E-mail address is not valid.");}/* Let's prepare the message for the e-mail */$message = "Hello!Your contact form has been submitted by:Name: $fullnameE-mail: $emailTel: $telComments:$commentsEnd of message";/* Send the message using mail() function */mail($myemail, $subject, $message);/* Redirect visitor to the thank you page */header('Location: thanks.php');exit();/* Functions we used */function check_input($data, $problem=''){	$data = trim($data);	$data = stripslashes($data);	$data = htmlspecialchars($data);	if ($problem && strlen($data) == 0)	{		show_error($problem);	}	return $data;}function show_error($myError){?>	<?php echo $myError; ?><?phpexit();}?>

Thanks for any help!

Link to comment
Share on other sites

The unexpected end message typically means that you have an open bracket without a closing one, and that looks like the case here.
Ah, I have modified the closing brackets, and now I get a different error. (Closer to the truth?)
Fatal error: Call to undefined function check_input() in /home/content/13/6882013/html/contactphp.php on line 19
So the recaptcha code is preventing the regular checks from being executed...
<?php  require_once('recaptchalib.php');  $privatekey = "xxxxxxxxxxxxxxxxxxxx";  $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	/* Set e-mail recipient */$myemail  = "info@test.com";/* Check all form inputs using check_input function */$fullname = check_input($_POST['fullname'], "Please enter your name.");$tel  = check_input($_POST['tel']);$email	= check_input($_POST['email']);$comments = check_input($_POST['comments'], "Please write your comments.");/* If e-mail is not valid show error message */if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){	show_error("It seems your E-mail address is not valid.");}/* Let's prepare the message for the e-mail */$message = "Hello!Your contact form has been submitted by:Name: $fullnameE-mail: $emailTel: $telComments:$commentsEnd of message";/* Send the message using mail() function */mail($myemail, $subject, $message);/* Redirect visitor to the thank you page */header('Location: thanks.php');exit();/* Functions we used */function check_input($data, $problem=''){	$data = trim($data);	$data = stripslashes($data);	$data = htmlspecialchars($data);	if ($problem && strlen($data) == 0)	{		show_error($problem);	}	return $data;}function show_error($myError){?>	<?php echo $myError; ?><?phpexit();}  }  ?>

Link to comment
Share on other sites

So the recaptcha code is preventing the regular checks from being executed...
No it's not. The function definitions are now inside an else block, so they don't get defined the same way the would if they were if they were outside of any block. When you put a function definition inside a conditional statement it only defines the function when execution reaches that line. If the function is defined outside of any other structure then it gets defined before execution begins.
Link to comment
Share on other sites

No it's not. The function definitions are now inside an else block, so they don't get defined the same way the would if they were if they were outside of any block. When you put a function definition inside a conditional statement it only defines the function when execution reaches that line. If the function is defined outside of any other structure then it gets defined before execution begins.
Thank you, that makes sense. How would I go about changing my verify.php?
Link to comment
Share on other sites

<?php  require_once('recaptchalib.php');  $privatekey = "xxxxxxxxxxxxxxxxxxxx";  $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	/* Set e-mail recipient */$myemail  = "info@test.com";function check_input($data, $problem=''){	$data = trim($data);	$data = stripslashes($data);	$data = htmlspecialchars($data);	if ($problem && strlen($data) == 0)	{		show_error($problem);	}	return $data;}function show_error($myError){?>   <?php echo $myError; ?><?phpexit();}/* Check all form inputs using check_input function */$fullname = check_input($_POST['fullname'], "Please enter your name.");$tel  = check_input($_POST['tel']);$email	= check_input($_POST['email']);$comments = check_input($_POST['comments'], "Please write your comments.");/* If e-mail is not valid show error message */if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){	show_error("It seems your E-mail address is not valid.");}/* Let's prepare the message for the e-mail */$message = "Hello!Your contact form has been submitted by:Name: $fullnameE-mail: $emailTel: $telComments:$commentsEnd of message";/* Send the message using mail() function */mail($myemail, $subject, $message);/* Redirect visitor to the thank you page */header('Location: thanks.php');exit();}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...