简体   繁体   English

Google Recaptcha v2带有电子邮件表格,显示http 500错误

[英]Google Recaptcha v2 With email form, gives http 500 Error

Using an html form for a "contact us". 使用html表单进行“与我们联系”。 This passes name, email, & message to a .php script and it works well. 这会将名称,电子邮件和消息传递到.php脚本,并且效果很好。 Add the Google recaptua v2 to this form gives a http 500 Error. 将Google recaptua v2添加到此表单会显示http 500错误。 This post and the code have been edited to reflect the KaplanKomputing tutorial suggested by Chris White. 这篇文章和代码已经过编辑,以反映Chris White建议的KaplanKomputing教程。

You can visit the working form without recaptcha, and nonworking recaptcha here: https://coinsandhistory.com#contact 您可以访问不带验证码和不带验证码的工作表格: https://coinsandhistory.com#contact

The "Google site key" I'll call here "XXXX-Google-site" and "YYYY-Google-secret". 我在这里将“ Google网站密钥”称为“ XXXX-Google-site”和“ YYYY-Google-secret”。

1st the contact form html, you don't need the css styling nor the stripslashes from the tutorial. 1st联系表格html,您不需要CSS样式,也不需要教程中的反斜杠。

<!DOCTYPE html>
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer> 
</script>

<link rel="stylesheet" href="../css/send-mail.css">
</head>

<body>
<!-- https://stackoverflow.com/questions/27188436/html-php-contact-form- 
email/55962553 -->
<!-- https://kaplankomputing.com/blog/tutorials/
recaptcha-php-demo-tutorial/ -->
<form action="send-mail_SO2_recapt.php" method="post" 
enctype="multipart/form-data" name="myemailform">
<div>
<span>Name &nbsp;</span>
<input type="text" name="name" value="" placeholder="Your Name">
</div>
<div>
<span>Email &nbsp;</span>
<input type="email" name="web_email" autocapitalize="off" 
autocorrect="off" 
value="" placeholder="youremail@domain.com">
</div>

<div>
<span>messgae &nbsp;</span>
<textarea name="message" placeholder="message"></textarea>
</div>

<!--  Google v2 Recaptua Form   -->
<div class="g-recaptcha" data-sitekey="XXXX-Google-site"></div>
<br/>

<div class="code">
<button><input type="submit" name="submit" value="Send"></button>
</div>
<i class="clear" style="display: block"></i>
</div>
</form>
</body>
</html>

And then the send-mail.php script. 然后是send-mail.php脚本。 I called mine "send-mail_SO2_recapt.php". 我称我为“ send-mail_SO2_recapt.php”。

<?php
/* error reporting, should rmv from working form */
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST["name"];
$visitor_email = $_POST['web_email'];
$message = $_POST["message"];
$response = $_POST["g-recaptcha-response"];

//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are needed!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}

$url = "https://google.com/recaptcha/api/siteverify";
$data = array(
"secret" => "YYYY-Google-secret",
"response" => $_POST["g-recaptcha-response"]);
$options = array(
"https" => array (
"method" => "POST",
"content" => https_build_query($data)
)
);
$context  = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);

if ($captcha_success=>success==false) {
echo "<p>You are a bot! Go away!</p>"; }
else if ($captcha_success=>success==true) {
echo "<p>You are not not a bot!</p>";   }

// $email_from = 'info@coinsandhistory.com';//<== update the email address
$email_from = "$visitor_email";
$email_subject = "New Form submission";
$email_body = "You have received a new message from $name.\n".
"sender's email:\n $email_from\n".
"Here is the message:\n $message";

$to = "youremail@yourdomain.com";   //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank_you_SO2.html');
exit;

// Function to validate against any email injection attempts
?>

If you supply code samples, please indicate what form it is: eg html, php, javascript. 如果您提供代码示例,请指出其形式:例如html,php,javascript。 I can't believe I'm the 1st person to try to use a simple Google recaptua in a contact form but this question doesn't appear plainly anywhere. 我不敢相信我是第一个尝试在联系表单中使用简单的Google Recaptua的人,但是这个问题在任何地方都不会出现。

i see number of errors in your code. 我在您的代码中看到了许多错误。 try the following code and see if it works, it is tested and working for me. 试试下面的代码,看看它是否有效,它已经过测试并且可以为我工作。 it is not based on your followed tutorial and uses curl for verification instead. 它不是基于您遵循的教程,而是使用curl进行验证。

Your biggest mistakes i think are that there is no isInfected function defined, => in place of -> and sometime file_get_contents doenst work on all servers. 我认为您最大的错误是没有定义isInfected函数, =>代替->并且有时file_get_contents在所有服务器上都起作用。

HTML: HTML:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form action="" method="post">
  <div>
    <span>Name</span>
    <input type="text" name="name" placeholder="Your Name" required>
  </div>
  <div>
    <span>Email</span>
    <input type="email" name="web_email" placeholder="youremail@domain.com" required>
  </div>
  <div>
    <span>Messgae</span>
    <textarea name="message" placeholder="message" required></textarea>
  </div>
  <!--  Google v2 Recaptcha Form   -->
  <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
  <div class="code">
    <input type="submit" name="submit" value="Send">
  </div>
</form>

PHP CODE: PHP代码:

<?php
//check form is submitted
if( isset($_POST['submit']) ){

  // get values
  $error = '';
  $name          = $_POST["name"];
  $visitor_email = $_POST['web_email'];
  $message       = $_POST["message"];

  //Validate first
  if(empty($name)||empty($visitor_email)) {
    $error = "Name and email are needed!";
  }

  //handle captcha response
  $captcha = $_REQUEST['g-recaptcha-response'];
  $handle = curl_init('https://www.google.com/recaptcha/api/siteverify');
  curl_setopt($handle, CURLOPT_POST, true);
  curl_setopt($handle, CURLOPT_POSTFIELDS, "secret=YOUR_SECRET_KEY&response=$captcha");
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($handle);
  $explodedArr = explode(",",$response);
  $doubleExplodedArr = explode(":",$explodedArr[0]);
  $captchaConfirmation = end($doubleExplodedArr);
  print_r($doubleExplodedArr);
  if ( trim($captchaConfirmation) != "true" ) {
    $error = "<p>You are a bot! Go away!</p>";
  }

  if( empty($error) ){ //no error
    // mail than
    $to = "youremail@mail.com";
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from ".$name.".\n".
    "sender's email:\n ".$visitor_email."\n".
    "Here is the message:\n ".$message;
    $headers = "From: ".$visitor_email." \r\n";
    $headers .= "Reply-To: ".$visitor_email." \r\n";
    //Send the email!
    $mail_check = mail($to,$email_subject,$email_body,$headers);
    if( $mail_check ){
      // echo "all is well. mail sent";
      header('Location: thank_you.html');
    } else {
      echo "mail failed. try again";
    }
  } else {
    echo $error;
  }
}
?>

Here is an answer which worked for me. 这是一个对我有用的答案。 I'd like to really thank Galzor as his answers helped me a lot. 我真的要感谢Galzor,因为他的回答对我很有帮助。 The base Code I got from Code Geek and I added stuff here to add in the form. 我从Code Geek获得的基本代码,并在此处添加了一些内容以添加到表单中。 This format hopefully eliminated the confusion on exactly what to include in the Google "SITE-KEY" and "SECRET-KEY" as it gets them as variables before processing them in a string. 这种格式有望消除对Google“ SITE-KEY”和“ SECRET-KEY”中包含的内容的困惑,因为它在将它们处理为字符串之前将它们作为变量获取。 These are actually 40 character strings. 这些实际上是40个字符串。 The sucessful captcha goes to a landing page. 成功的验证码转到登录页面。

This is the HTML send-mail_form.html 这是HTML send-mail_form.html

<!DOCTYPE html>
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

</head>

<body>
<!-- form goes in the body of HTML  -->
<form action="send-mail_form.php" method="post">

<div>
<span>Name</span>
<input type="text" name="name" value="" placeholder="Your Name" required>
</div>

<div>
<span>Email</span>
<input type="email" name="web_email" placeholder="youremail@domain.com" required>
</div>
<div>
<span>Messgae</span>
<textarea name="message" placeholder="message" required></textarea>
</div>

<!--  Google v2 Recaptcha Form   -->
<div class="g-recaptcha" data-sitekey="SITE-KEY"></div>
<div class="code">
<input type="submit" name="submit" value="Send">
</div>
</form>

</body>
</html>

And this will be the called send-mail_form.php. 这将被称为send-mail_form.php。 I won't bother with showing the thank_you_SO2.html here. 我不会在这里显示thank_you_SO2.html。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$web_email;$message;$captcha;
// check form is submitted
if(isset($_POST['web_email']) ){

// get values
$name=            $_POST["name"];
$visitor_email=   $_POST['web_email'];
$message=         $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) {
$error = "Name and email are needed!";
}

if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}

if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}

$secretKey = "SECRET-KEY";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . 
urlencode($secretKey) .  '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
// echo '<h3>Thanks for contacting us</h3>';

// mail then
$to = "youremail@yourdomain.com";
$email_subject = "CG Recaptcha Form2 submission";
$email_body = "You have received a new message from ".$name.".\n".
"sender's email:\n ".$visitor_email."\n".
"Here is the message:\n ".$message;

//Send the email!
$mail_check = mail($to,$email_subject,$email_body);
if( $mail_check ){
// echo "all is well. mail sent";
header('Location: thank_you_SO2.html');
}
else {
echo '<h2>You are a spammer ! Go Away</h2>';
}
}
}
?>

There are some unneccesary items, the error checking at the top can probably be removed. 有一些不必要的项目,可能可以除去顶部的错误检查。 Also will the Google site verify will work with https://google.com/recaptcha/api/siteverify?secret= .... ? Google网站还将验证是否可以与https://google.com/recaptcha/api/siteverify?secret= ....一起使用吗? Actually on testing it seems to fail sometimes without the www so perhaps best to keep it. 实际上,在测试中,有时如果没有www,它似乎会失败,因此最好保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM