PHP recaptcha不工作
我已经在这个网站上设置了google reCaptcha PHP插件: 您可以看到它在Contact下显示良好,但是在将必要的PHP添加到我的form_流程文件后,无论是否填写了reCaptcha,表单仍会提交。下面是我的PHP代码,它位于form_进程文件中:PHP recaptcha不工作,php,recaptcha,Php,Recaptcha,我已经在这个网站上设置了google reCaptcha PHP插件: 您可以看到它在Contact下显示良好,但是在将必要的PHP添加到我的form_流程文件后,无论是否填写了reCaptcha,表单仍会提交。下面是我的PHP代码,它位于form_进程文件中: <?php //Check if POST data is set, and not empty, else it will do this every single time, submitted or not
<?php
//Check if POST data is set, and not empty, else it will do this every single time, submitted or not
require_once('recaptchalib.php');
$privatekey = "6Le2a_oSAAAAAJ81_yQvCelFMIHiUcG_k6u0S1fd";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
if(isset($_POST) && !empty($_POST))
{
$mail_to = 'benliger@hotmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'enquiry ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Message: ' . $message;
// Construct email headers
$headers = 'From: ' . $mail_from . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){
//Echo the message now, because it will be catched in your jQuery listerener (see code below)
echo 'Thanks for getting in touch!';
} else {
//Echo the message now, because it will be catched in your jQuery listerener (see code below)
echo 'Message not sent :( Please, get in contact with me directly: benliger@hotmail.com';
}
//This exit; is important, else the alert box will be full of the further html code
exit;
}
}
?>
错误在jquery代码中。无论您的响应是什么,都要用
感谢您加入touchaaa
使用警报(responseMessage)
查看响应
因此,请替换$(“.mycontactform”).html(“感谢您进入touchaaa!”)代码>
使用警报(responseMessage)
生成时是否使用了正确的键?我也找不到问题。请尝试将require更改为require。在检查请求是否通过POST发出之前,您正在检查验证码的有效性…这没有什么意义。您能打印$resp的输出吗?我尝试了Imran,但没有成功,正如你可以告诉我的,我对PHP非常陌生,所以不完全确定逻辑是否正确,只是尽量按照谷歌文档中的说明进行操作@CBroe你能详细说明一下它应该是什么样子吗?解决了!我现在唯一的问题是,我是否可以拥有它,以便在提交表单时正确填写验证码,它会像以前一样将消息回显到html,而不是通过alert javascript框?$(“.mycontactform”).html(responseMessage);最后一个问题,不要假设有办法让失败表单中的响应消息以警报(responseMessage)的形式出现,并让成功的消息以html的形式发布,就像你的建议一样?在服务器上创建响应数组,并通过函数json\u dump(array)返回json,在jquery函数中检查对应的元素
<form name="myForm" action="form_process.php" method="POST">
<?php
require_once('recaptchalib.php');
$publickey = "6Le2a_oSAAAAAEHu4u35QWlLzxzCYB1JnhFoI0u5"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input class="sendbutton" type="submit" name="send_message" value="Send">
$(document).on('submit','form',function(e){
//Prevent the default action
e.preventDefault();
var form = $(this);
//Create an array of input values
var data = $(this).serializeArray();
//Do the ajax request
$.post('form_process.php',data,function(responseMessage){
resetForm(form);
//Alert your message
$( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');
//alert(responseMessage);
});
});