php - 如何使用 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>';
}
?>
解决方案
好的,所以为了简化流程并去除中间检查,我们可以在同一个文件上进行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
推荐阅读
- reactjs - ReactJS:我如何知道哪个依赖项使 useEffect 挂钩运行/触发?
- json - 将对象推送到 SQL Server 中的 json 数组
- django - Django REST RetrieveUpdateAPIView 问题
- protocol-buffers - MSYS2 endian.h 构建协议缓冲区时没有这样的文件或目录
- highcharts - 在 highcharts 水平条形图中,我无法显示默认刻度
- node.js - 部署后的 GraphQL 拒绝连接
- vue.js - 当我设置哈希属性时,Vue路由器不滚动
- python - Selenium - WebDriverWait 直到找到保存在列表中的任何 xpath
- java - 使用 Wildfly Elytron 安全的程序化登录
- java - android studio中的依赖微调器