我已经通过Dreamweaver创建了一个网站,该网站带有一个简单的联系表来发送电子邮件。以前,它没有重新安装并可以正常工作,可以发送电子邮件并正确显示成功/失败消息。 我现在使用Recaptcha v2复选框将其重写,但是现在总是收到错误消息“无效的验证码”,并且没有电子邮件。 页面显示正确。 验证码显示正确,允许您进行选择,然后单击验证按钮,从而显示绿色的勾号。但是,单击“发送”按钮时,它总是失败。
任何帮助表示感谢。
实时网站:https://www.simpleenglish.info/en/contact_us/contact_form.php
我确认站点密钥和秘密密钥以及所有电子邮件地址都是正确的。我已经删除并创建了密钥,并两次通过google admin关联了域,但没有成功。我相信代码是正确的(没有明显的错误)。我在这里或Google网上论坛都没有找到任何类似的问题来指明我的方向。我不知道还能去哪里。
contact_form.php代码(现已完成):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/SE1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Simple English Contact</title>
<script src="https://www.google.com/recaptcha/api.js"></script>
<!-- InstanceEndEditable -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-136785269-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-136785269-1');
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-1086244781484067",
enable_page_level_ads: true
});
</script>
<link href="https://www.simpleenglish.info/SE_3ColLiquid.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
ul.nav a { zoom: 1; } /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */
</style>
<![endif]-->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>
<body>
<div class="container">
<div class="sidebar1"> <a href="https://www.simpleenglish.info/index.html"><img src="/images/logo.jpg" alt="Simple English Logo" width="99%"/></a>
<ul class="nav" >
<li><a href="https://www.simpleenglish.info/index.html">Home</a></li>
<li><a href="https://www.simpleenglish.info/en/vocabulary.html">Vocabulary</a></li>
<li><a href="https://www.simpleenglish.info/en/usefullinks.html">Useful Links</a></li>
<li><a href="https://www.simpleenglish.info/en/teachersaids.html">Teacher's Aids</a></li>
<li><a href="https://www.simpleenglish.info/en/contact_us/contact_form.php">Contact us</a></li>
<li><a href="https://www.simpleenglish.info/en/sitemap.html">Site Map</a></li>
</ul>
</div>
<div class="content">
<img src="https://www.simpleenglish.info/images/title.jpg" width="99%" alt="Simple English Title"/>
<br />
<br />
<!-- InstanceBeginEditable name="EditRegionContent" -->
<h1 class="pageheader">Contact Us</h1>
<p>To send us a message, please enter <strong>all the information</strong> in the following form, then click the 'Send' button.</p>
<div class="contact_us">
<form method="post" action="" accept-charset="UTF-8">
<table>
<tr><td>Name:</td><td><input type="text" name="sendername" placeholder="Enter your full name" size="50" required="required"/></td></tr>
<tr><td>Email:</td><td><input type="email" name="senderemail" placeholder="Enter your email address" size="50" required="required"/></td></tr>
<tr><td>Message Context:</td><td><input type="text" name="sendercontext" placeholder="this will be context" size="50" required="required"/></td></tr>
<tr><td>Message:</td><td><textarea name="sendermessage" rows="5" cols="50" placeholder="How can we help you?" required="required"></textarea></td></tr>
<tr><td>Spam Check:</td><td><div class="g-recaptcha" data-sitekey="XXX Correct Key XXX"></div></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Send" class="submit-btn"/></td></tr>
</table>
</form>
<div class="status">
<?php if(isset($_POST['submit']))
{
$user_name = $_POST['sendername'];
$user_email = $_POST['senderemail'];
$user_context = $_POST['sendercontext'];
$user_message = $_POST['sendermessage'];
$email_from = 'contact@simpleenglish.info';
$email_subject = "Simple English Contact";
$email_body = "Name: $user_name.\n".
"Email: $user_email.\n".
"Context: $user_context.\n".
"Message: $user_message.\n";
$to_email = "admin@simpleenglish.com";
$header = "From: $email_from \r\n";
$header .= "Reply-To: $user_email \r\n";
$secretkey = "XXX Correct Key XXX";
$responsekey = $_POST['g-recaptcha-response'];
$userip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretkey&response=$responsekey&remoteip=$userip";
$response = file_get_contents($url);
$response = json_decode($response);
if($response->success)
{
mail($to_email,$email_subject,$email_body,$header);
echo "Message sent successfully";
}
else
{
echo "Invalid Captcha. Please try again.";
}
}
?>
</div>
</div>
<p> </p>
<p> </p>
<!-- InstanceEndEditable -->
<p> </p>
<!-- end .content --></div>
<div class="sidebar2">
<h4> </h4>
<p> </p>
<!-- end .sidebar2 --></div>
<div class="footer">
<p>Copyright © 2018<br />
All rights reserved <!-- end .footer --></p>
</div>
<!-- end .container --></div>
</body>
<!-- InstanceEnd --></html>
答案 0 :(得分:0)
您的Recaptcha的div标签未正确关闭。查看页面源代码,您将在表格行中看到它。
<tr><td>Spam Check:</td><td><div class="g-recaptcha" data-sitekey="6Lc8OZ8UAAAAABUosvouUbamfotW9Pp-9ioRIEOZ"></td></tr>
答案 1 :(得分:0)
因为我使用类,所以将其放入函数中。 Google希望该方法为POST。您可以通过设置file_get_contents
选项来使用stream_context_create
。
其余的内容很容易解释,$secretkey
是您的身份验证密钥,$captcha
是您的$_POST['g-recaptcha-response']
https://www.php.net/manual/en/function.stream-context-create.php
function checkRecaptcha( $secretkey , $captcha ){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => $secretkey ,
'response' => $captcha ,
'remoteip' => $_SERVER['REMOTE_ADDR'] ,
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( $data )
]
];
$context = stream_context_create( $options );
$contents = file_get_contents( $url , false , $context );
$response = json_decode( $contents , true ); //convert to assoc array
// if you want to use objects, remove true from json decode and then use $response->success
if( ! $response["success"] ) {
return false;
}
return true;
}
然后您可以像这样使用它:
<?php
function checkRecaptcha( $secretkey , $captcha ){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => $secretkey ,
'response' => $captcha ,
'remoteip' => $_SERVER['REMOTE_ADDR'] ,
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( $data )
]
];
$context = stream_context_create( $options );
$contents = file_get_contents( $url , false , $context );
$response = json_decode( $contents , true ); //convert to assoc array
// if you want to use objects, remove true from json decode and then use $response->success
if( ! $response["success"] ) {
return false;
}
return true;
}
if( isset($_POST['submit'] ) ) {
$secretkey = "XXX Correct key here XXX";
if( ! checkRecaptcha( $secretkey , $_POST['g-recaptcha-response'] ) ) {
//only set variables if passes recaptcha
//validate email address
if ( ! filter_var( $_POST['senderemail'] , FILTER_VALIDATE_EMAIL)) {
echo "Please provide a valid email";
die();
}
//ensure user doesn't post HTML
$user_name = strip_tags ( $_POST['sendername'] );
$user_context = strip_tags ( $_POST['sendercontext'] );
$user_message = strip_tags ( $_POST['sendermessage'] );
$email_body = "Name: $user_name.\n"."Email: {$_POST['senderemail']}.\n"."Context: $user_context.\n"."Message: $user_message.\n";
$header = "From: bla@simpleenglish.info \r\n";
$header .= "Reply-To: {$_POST['senderemail']} \r\n";
mail( 'bla@simpleenglish.com' , 'Simple English Contact' , $email_body , $header );
echo "Message sent successfully";
}else{
echo "Invalid Captcha. Please try again.";
}
}
?>