将reCAPTCHA v2集成到现有表单

将reCAPTCHA v2集成到现有表单,recaptcha,Recaptcha,我在将reCAPTCHA v2与现有表单集成时遇到问题。如果有人能解决这个问题,我将不胜感激 这是我的表单页面HTML代码,我在其中添加小部件: 前/头标签: <script src="https://www.google.com/recaptcha/api.js" async defer></script> 在正文中: <div class='mod modContactForm'>

我在将reCAPTCHA v2与现有表单集成时遇到问题。如果有人能解决这个问题,我将不胜感激

这是我的表单页面HTML代码,我在其中添加小部件:

前/头标签:

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

在正文中:

            <div class='mod modContactForm'>
              <form method="post" action="send.php" accept-charset="UTF-8"><p>
                <label for='name'>Your Name</label>
                <input class="input-text required" id="name" name="name" type="text" />
              </p>
              <p>
                <label for='email'>Your Email</label>
                <input class="input-text required email" id="email" name="email" type="text" />
              </p>
              <p>
                <label for='message'>Your Message</label>
                <textarea class="required" rows="5" cols="80" id="message" name="message"></textarea>
              </p>
<form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="--- MY SITE KEY HERE ---"></div>
      <input class="button" type="submit" value="Send message">
              <p class='thanks' style='display: none;'>
                Thanks for contacting Video Production Edinburgh,<br />we'll be in touch within 24 hours!
              </p>
              </form>


你的名字

你的电子邮件

你的信息

感谢您联系爱丁堡视频制作部,
我们将在24小时内保持联系!

可以找到不工作的reCAPTCHA的活动页面

这是服务器端处理表单提交的send.php代码:

<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '--- MY SECRET KEY HERE ----',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if (!$res['success']) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else {
        // If CAPTCHA is successfully completed...

        $to = "hello@videoproductionedinburgh.co.uk";
  
  $name = $_POST['name'];
  $subject = $_POST['subject'];
  
  $body = stripslashes($_POST['message']);
  
  $from = $_POST['email'];
  $headers = "Reply-To: " . $from;
  $error_messages = array();

  if( strlen($name) < 1 ) {
    $error_messages[] = "Name required";
  }

  if( strlen($subject) < 1 ) {
    // set the default subject here
    $subject = "Message from Video Production Edinburgh";
  }

  if( strlen($body) < 1 ) {
    $error_messages[] = "Message required";
  }

  if( strlen($from) < 1 ) {
    $error_messages[] = "Email required";
  }

  if(sizeof($error_messages) == 0 && mail($to, $subject, $body, $headers)) {
    echo("success");
  } else {
    foreach( $error_messages as $error_message ) {
      echo "$error_message <br />";
    }
  }

    }
?>