首页 > 解决方案 > 如何使用 Google Recaptcha POST 将这个 PHP 表单制作到不同的 URL?

问题描述

我必须使用 Google reCaptcha 创建一个 PHP 表单。表单必须 POST 到特定的 URL 以处理提交,并且当表单提交时,它不能将用户重定向到另一个页面。

我找到了表单的基本示例、java 和 PHP 代码。我需要帮助将提交数据添加到不同的 URL。

我如何使用 Google Recaptcha 制作这个 PHP 表单,发布到另一个 URL 并且 reCaptcha 仍然有效?我需要将表单的数据提交到特定的 URL,然后该 URL 将处理提交。

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // access
    $secretKey = '___enter_secret_key___';
    $captcha = $_POST['g-recaptcha-response'];

    if(!$captcha){
      echo '<p class="alert alert-warning">Please check the the captcha form.</p>';
      exit;
    }

    # FIX: Replace this email with recipient email
    $mail_to = "demo@gmail.com";

    # Sender Data
    $subject = trim($_POST["subject"]);
    $name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = trim($_POST["phone"]);
    $message = trim($_POST["message"]);

    if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($phone) OR empty($subject) OR empty($message)) {
        # Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo '<p class="alert alert-warning">Please complete the form and try again.</p>';
        exit;
    }

    $ip = $_SERVER['REMOTE_ADDR'];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
    $responseKeys = json_decode($response,true);

    if(intval($responseKeys["success"]) !== 1) {
      echo '<p class="alert alert-warning">Please check the the captcha form.</p>';
    } else {
        # Mail Content
        $content = "Name: $name\n";
        $content .= "Email: $email\n\n";
        $content .= "Phone: $phone\n";
        $content .= "Message:\n$message\n";

        # email headers.
        $headers = "From: $name <$email>";

        # Send the email.
        $success = mail($mail_to, $subject, $content, $headers);
        if ($success) {
            # Set a 200 (okay) response code.
            http_response_code(200);
            echo '<p class="alert alert-success">Thank You! Your message has been sent.</p>';
        } else {
            # Set a 500 (internal server error) response code.
            http_response_code(500);
            echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
        }
    }

} else {
    # Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo '<p class="alert alert-warning">There was a problem with your submission, please try again.</p>';
}

 ?> 

标签: phprecaptcha

解决方案


好的,所以为了简化流程并去除中间检查,我们可以在同一个文件上进行recatptcha验证和表单提交处理。使用以下 php 代码验证您的验证码结果并进行相应操作。

PHP(表单提交处理文件):

if(isset($_REQUEST['your_form_submit'])){

  $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);

  if(trim($captchaConfirmation) == "true") {

    //DO YOUR STUFF HERE TO PROCESS SUBMISSIONS    

  } else {
    echo "<script> alert('Captcha entry was wrong. Please try again') </script>";
  }

} //end

推荐阅读