Php GoogleRecapTChaV2带有电子邮件表单,给出http 500错误
使用html表单“联系我们”。这会将name、email和message传递到一个.php脚本,它工作得很好。如果将Google recaptua v2添加到此表单,则会出现http 500错误。这篇文章和代码都经过了编辑,以反映ChrisWhite建议的Kaplan计算教程 您可以在不使用recaptcha的情况下访问工作表单,也可以在此处访问非工作recaptcha: “谷歌网站密钥”我在这里称为“XXXX谷歌网站”和“YYYY谷歌秘密” 首先,使用联系人表单html,您不需要css样式,也不需要教程中的斜杠Php GoogleRecapTChaV2带有电子邮件表单,给出http 500错误,php,html,recaptcha,contact-form,Php,Html,Recaptcha,Contact Form,使用html表单“联系我们”。这会将name、email和message传递到一个.php脚本,它工作得很好。如果将Google recaptua v2添加到此表单,则会出现http 500错误。这篇文章和代码都经过了编辑,以反映ChrisWhite建议的Kaplan计算教程 您可以在不使用recaptcha的情况下访问工作表单,也可以在此处访问非工作recaptcha: “谷歌网站密钥”我在这里称为“XXXX谷歌网站”和“YYYY谷歌秘密” 首先,使用联系人表单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 </span>
<input type="text" name="name" value="" placeholder="Your Name">
</div>
<div>
<span>Email </span>
<input type="email" name="web_email" autocapitalize="off"
autocorrect="off"
value="" placeholder="youremail@domain.com">
</div>
<div>
<span>messgae </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>
名称
电子邮件
梅斯盖
然后是send-mail.php脚本。我把我的名字叫做“send-mail\u SO2\u repact.php”
我看到您的代码中有很多错误。尝试下面的代码,看看它是否工作,它是测试和工作为我。它不是基于您所遵循的教程,而是使用curl进行验证
我认为您最大的错误是没有定义isInfected函数,=>代替->并且有时文件内容在所有服务器上都不起作用
HTML:
名称
电子邮件
梅斯盖
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;
}
}
?>
这是一个对我有用的答案。我真的要感谢Galzor,因为他的回答对我帮助很大。我从代码极客那里得到的基本代码,我在这里添加了一些东西来添加到表单中。这种格式有望消除谷歌“SITE-KEY”和“SECRET-KEY”中到底应该包含什么的混淆,因为它在处理字符串之前将它们作为变量。这实际上是40个字符串。成功的验证码进入登录页
这是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>
名称
电子邮件
梅斯盖
这就是所谓的send-mail_form.php。我不会在这里显示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>';
}
}
}
?>
Tutorial:谢谢,我按照您的示例所概述的思路编辑了HTML和PHP代码。虽然它仍然不起作用,但我觉得我离它更近了。我了解了如何将中的recaptcha表单数据与请求姓名、电子邮件和消息的原始表单数据相结合。它还显示了在哪里添加Google密钥的“秘密响应”并进行比较。然而,我现在得到一个HTTP500错误。注意,我将kaplankomputing代码中的所有引用从http更改为https。哦,作为参考,您可以在这里看到工作的普通表单和非工作的recaptcha表单:非常感谢!然而,我得到了一个新的错误。这是数组([0]=>{“success”[1]=>false)你是一个机器人!走开!我应该说我还在学习javascript,还没有开始学习函数。在php文件中,如果我将if(trim)测试改为:if(trim($captchaConfirmation)!=“false”){$error=“你是一个机器人!走开!”;然后无论复选框是否选中,重述始终成功。我确实返回了谷歌,并确保我直接从谷歌复制了数据站点密钥和机密。它们以原始形式显示正确。如果您获得数组([0]=>{“成功”[1]=>false),这意味着google没有将u作为人传递。我添加了print_r($doubleExplodedArr);行以显示来自recaptcha本身的原始响应。我认为代码中没有错误。可能尝试在同一页面上提交,而不是离开表单action=“”空,并在同一页上编写php代码。如果您对此感到失望,请随时发送电子邮件至aspnetusername@gmail.com.good,看起来你走的方向是对的。我再次测试了整个代码,我为你提供了另一个recaptcha密钥,它工作得很好。因此,它肯定是这个cod中的另一个这个问题阻碍了它的工作。我很高兴它成功了。干得好。希望你在过程中学到了很多。非常感谢你的帮助Galzor。你关于php电子邮件标题的文章非常有用。下面是参考:我添加的代码是$headers.=”来自:myemail@mydomain.com\r\n“;注意$headers后面的空格和句点。这是必需的,因为ISP的服务器不信任来自未知来源的邮件。
<!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>
<?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>';
}
}
}
?>