我正在为我的客户整合一个新的Google recaptcha,我厌倦了我的工作服务器,一切正常,但在客户端服务器上,它是go(!$ captcha)部分。
似乎每次Json都回答错误,我尝试了不同的方法,但失败了。
这是我的代码:
ob_start();
require("class.phpmailer.php");
$captcha = $_POST['g-recaptcha-response'];
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = ""; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$Name = $_REQUEST['name'];
$Email = $_REQUEST['email'];
$Subject = $_REQUEST['subject'];
$Remarks = $_REQUEST['remarks'];
$mail->From = "";
$mail->FromName = "Vision Pilates Enquiry Form";
$mail->AddAddress("", "Admin");
//$mail->AddAddress("ellen@example.com"); // name is optional
$mail->AddReplyTo("$Email", "$Name");
$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
//$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Enquiry from website: $Subject";
$mail->Body = "<strong>Name:</strong> $Name <br><strong>Email:</strong> $Email <br><strong>Subject:</strong> $Subject <br><strong>Remarks:</strong> $Remarks <br>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if($_SERVER["REQUEST_METHOD"] === "POST") {
//form submitted
//check if other form details are correct
//verify captcha
if (!$captcha) {
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret==SecretKey=&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if ($response . success == false) {
echo '<h2>You are spammer ! Get the @$%K out</h2>';
} else {
if (!$mail->Send()) {
header("location: error.htm");
exit;
} else {
header("location: sent.htm");
}
}
}
提交最终结果后
由于
答案 0 :(得分:1)
post
API请求
网址:https://www.google.com/recaptcha/api/siteverify
方法:POST
function VerifyRecaptcha($g_recaptcha_response) {
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://www.google.com/recaptcha/api/siteverify",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'secret' => 'your secret goes here <<<<<<<',
'response' => $g_recaptcha_response,
'remoteip' => $_SERVER['REMOTE_ADDR']
)
);
curl_setopt_array($ch, $curlConfig);
if($result = curl_exec($ch)){
curl_close($ch);
$response = json_decode($result);
return $response->success;
}else{
var_dump(curl_error($ch)); // this for debug remove after you test it
return false;
}
}
然后像那样使用它
if(!VerifyRecaptcha($_POST['g-recaptcha-response']))
{
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
答案 1 :(得分:0)
你正在以有趣的顺序做事。在设置其他任何内容之前检查验证码 - 如果验证码错误,您将不需要PHPMailer实例。
我还怀疑您的网址已损坏:
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret==SecretKey=&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
你有一个额外的=
,你没有逃脱你的参数。这可能导致URL无效 - 重新访问通常会要求两个单词,并且空格在URL中无效,所以我建议你这样做:
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SecretKey=&response=" . rawurlencode($captcha) . "&remoteip=" . rawurlencode($_SERVER['REMOTE_ADDR']));
您还使用旧版本的PHPMailer;请get the latest。