Jump to content

Error after trying to update reCAPTCHA


Janell

Recommended Posts

I am submitting an email form that posts to a php page. The form is being sent, I'm receiving the email but I am getting "The page cannot be displayed because an internal server error has occurred." message rather than the landing/success page.  I needed to update the reCAPTCHA on the site. The only thing I've changed/added in the code is the part at the bottom, starting at $url. I'm not familiar with php, any help would be appreciated.

<?php 

 $email = "email@email.com";
 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Online Contact Form"; 

 $fields = array(); 
 $fields{"Name"} = "Name";  
 $fields{"Email"} = "Email"; 
 $fields{"Phone"} = "Phone"; 
 $fields{"Company"} = "Company"; 
 $fields{"Question"} = "Message"; 
 
 $body = "We have received a new submission from the Contact form\n\n"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 
 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else { 
 $send = mail($email, $subject, $body, $headers);  if($send) 
 {echo "Thank you! Your email has been sent.<br>
We will get back to you as soon as we can. <br>
<a href='http://www.home/index.html'>Go to Home</a>"; }
 else 
 {print "We encountered an error sending your mail, please notify webmaster@website.com."; } 
 }
 }
 
 	$url = 'https://www.google.com/recaptcha/api/siteverify';
	$data = array(
		'secret' => 'xyz',
		'response' => $_POST["g-recaptcha-response"]
	);
	$options = array(
		'http' => array (
			'method' => 'POST',
			'content' => http_build_query($data)
		)
	);
	$context  = stream_context_create($options);
	$verify = file_get_contents($url, false, $context);
	$captcha_success = json_decode($verify);
 ?>

Link to comment
Share on other sites

I'm guessing the reCAPTCHA code should come before the mail is sent - and that you need to check the result to make sure it validated before sending the email.

 

As for the error, there could be so many reasons. Maybe your server is configured to not allow URLs to be passed to file_get_contents? Maybe $_POST["g-recaptcha-response"] isn't set (did you add it to your form?). Maybe there's some reason file_get_contents is taking too long to load the URL (bad connection to the server, the server is blocking the request, etc) and the script is timing out? That last one will be easy to rule out depending on whether you get the error quickly or if the page loads for ~30 seconds or so.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.