Jump to content

Error after trying to update reCAPTCHA


Janell
Go to solution Solved by 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.