这是我的enquiry.php文件,您可以看到的代码是recaptcha框/表单,因为您将看到我已经包含了我的响应操作,如果重新访问框是&n,则show_error函数输出#39; t勾选 - 这是有效的,但如果勾选方框并且表格填写正确,则不会将我重定向到thank-you.html
<?php
$email_to = "billy.farroll@hotmail.com";
if($_POST){
$name;
$telephone;
$email;
$comment;
$captcha;
if(isset($_POST['name']))
$name=$_POST["name"];
if(isset($_POST['telephone']))
$telephone=$_POST["telephone"];
if(isset($_POST['email']))
$email=$_POST["email"];
if(isset($_POST['comment']))
$comment=$_POST["comment"];
if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
if (empty($name))
{
show_error("Oops! you forgot to fill in your name, Please go back to the homepage and try again");
}
if (empty($telephone))
{
show_error("Oops! you forgot to fill in your telephone, Please go back to the homepage and try again");
}
if (empty($email))
{
show_error("Oops! you forgot to fill in your email, Please go back to the homepage and try again");
}
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || (!$captcha))
{
show_error("Please tick the box");
}
} else {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Lf2sEIUAAAAAMio98GU_V6gki_mGxS3Z6WMBmA9&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false)
{
show_error("Please tick the box");
}
else
{
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Enquiry Form";
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message =
"Name: ". $name .
"\r\nTelephone Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\n\r\n\r\nComments :" . $comment;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header ('location: thank-you.html');
}
}
}
?>
这是我的表单HTML代码,您可以看到我已经在下面插入了我的recaptcha框代码以及链接到我的enquiry.php文件
<form id="enquiry-form" action="enquiry.php" style="text-align:left; font-size:10px; margin-top: 20px;" method="post">
<fieldset style="border:#FFF; font-size:12px; background-color:#0d5ea9;">
<table width="100%" border="0" cellpadding="0" style="margin-top:15px;">
<tr>
<td class="tablebutton">Name:</td>
<td><input type="text" name="name" id="name" size="50" /></td>
<td class="tablebutton">Telephone:</td>
<td><input type="text" name="telephone" id="telephone" size="37" /></td>
</tr>
<tr>
<td class="tablebutton">Email:</td>
<td> <input type="text" name="email" id="email" size="50" /></td>
</tr>
</table>
<div class="hide-for-small-only" style="margin-bottom:10px; font-size: 18px; font-family: 'Saira Semi Condensed', sans-serif; margin-left:10px; color:#FFFFFF; ">Comments</div>
<div align="left" class="hide-for-small-only" style="margin-left:10px;"><textarea name="comment" id="comment" cols="30" rows="10" style="width:50%; float:left;"></textarea>
<!--I'm not a robot box -->
<div class="g-recaptcha" data-sitekey="6Lf2sEIUAAAAAPyFiim58oXAdkgOS-m5dPJd2f1g"></div>
<!--I'm not a robot box -->
<div class="contactMirror"><strong>Contact Information:</strong><br />
<br>
If you have any other queries regarding the site, it's functionality or any technical questions about our vinyl service, please do not hesitate to contact us as we are always happy to help.
<br>
<br>
Please fill in the form and we will get straight back to you.
</div>
</div>
<br />
<br /> <br />
<div class="submitbutton" align="center" style="float:left; margin-top: 75px;"><button type="submit"><strong>Send</strong></button></div>
</fieldset>
</form>
答案 0 :(得分:0)
我找到了这个问题的答案,请参阅修改后的PHP代码:
$captcha = $_POST["g-recaptcha-response"]; // Declare variable
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
header("Location: <!-- Error HTML page -->");
exit;
}
$secretKey = "<!-- SECRET KEY HERE -->";
$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 '<h2>Spam Detected</h2>';
}
else {
header("Location: <!-- Thank you HTML page -->");
}
// NEW RECAPTCHA