php - 现有表单的 Recaptcha ... php 部分给我带来了问题
问题描述
我不是一个真正的编码员,但有点了解......买了一个带有联系表格的网络模板。它将表单发送到 php 进行验证,然后通过电子邮件将其发送给我。我可以让 Recaptcha 在表单上显示得很好......但是 php 似乎失败了(给我一个 500 错误,但它确实向我发送了表单电子邮件)......对不起......我正在撕裂我的掉头发!
以下是php文件:
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => 'secret_code_here',
'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);
if ($captcha_success->success==false) {
echo "<p>You are a bot! Go away!</p>";
} else if ($captcha_success->success==true) {
use VanillaForm\MailSender;
use VanillaForm\Utils;
require_once('inc/Utils.php');
require_once('inc/MailSender.php');
header('Access-Control-Allow-Origin: *');
/*
* Check if call is a POST request (data was sent by form).
* While it's not a POST request it returns OK, which can be handy with checking that the script is alive.
*/
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
if (function_exists('mail')) {
die('OK');
} else {
die('PHP parser works, but <b>mail()</b> function seems to doesn\'t exist');
}
}
/*
*
* Vanilla Configuration starts HERE
*
*/
$vf_config = array(
/*** Code Snippet - quick-start ***/
/**
* Recipient's e-mail. To this e-mail email will be sent.
* E.g. Single recipient
* 'emailRecipients' => 'john@domain.com',
*
* E.g. Multiple recipients
* 'emailRecipients' => 'john@domain.com, andy@domain.com',
*/
'emailRecipients' => 'my@email.com',
/**
* If is not empty it sets a header From in e-mail message (sets sender e-mail).
* Note: some hosting servers can block sending e-mails with custom From field in header.
* If so, leave this field as empty.
* E.g. Single recipient
* 'emailSender' => 'john@domain.com',
*/
'emailSender' => 'my@email.com'
/*** /Code Snippet - quick-start ***/
);
$vfSender = new MailSender($vf_config);
/*
* Some variable may need translation for better readability.
* You can access and modify variables by:
* $vfSender->data['variable-name']
*/
switch ($vfSender->data['gender']) {
case "F":
$vfSender->data['gender'] = "Female";
break;
case "M":
$vfSender->data['gender'] = "Male";
break;
default:
$vfSender->data['gender'] = "Not selected";
break;
}
// Define here subject of the e-mail message
$subject = 'Website Contact Form - New Message from '.$vfSender->data['name'];
// Define here content of the e-mail message
$content = "Hey,
You've received new message from your website. Check the details below:
Sender's IP address: ".Utils::getIp()."
Name: {NAME}
E-mail: {EMAIL}
Phone number: {TEL}
Subject: {SUBJECT}
Message:
{MESSAGE}
";
// Set subject and message content
$vfSender->setMessage($subject, $content);
/*
* Vanilla Configuration ends HERE
*/
die($vfSender->sendMessage());
}
解决方案
推荐阅读
- css - 设置与其他 div 相同的高度,不使用 javascript,并且在 flexbox 内
- prometheus - 带有前缀的所有普罗米修斯指标的 Grafana 变量
- java - DownloadManager 在 Android Q 上不起作用 - Android/Java
- python - 如何提高图像分类器的准确性?
- kotlin - 你如何解决 Kotlin 中的循环导入
- python-3.x - How to get indices of drag and drop in PyQt5
- php - whereMonth 未能按月检索约会日期
- scala - Scala Play (2.8) 项目:从 git 克隆后,sbt run 产生:java.lang.RuntimeException:未检测到主类
- spring - Cannot deserialize instance of `com.example.million.model.Domain` out of START_ARRAY token
- laravel - Laravel 代理客串