我正在为我的客户集成一个新的Google,我厌倦了我的工作服务器,一切正常工作,但是在客户端服务器上,它的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");
}
}
}
提交后的最终结果是
请查一下卡普查表格。
。
谢谢
发布于 2016-12-29 11:49:13
该方法必须根据post
的recaptcha文件
API请求 网址:https://www.google.com/recaptcha/api/siteverify 方法:员额
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;
}
发布于 2015-04-13 22:42:48
你做事情的顺序很有趣。在设置其他内容之前检查captcha --如果captcha错误,则不需要PHPMailer实例。
我还怀疑您的URL被破坏了:
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret==SecretKey=&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
你有一个额外的=
在那里,你不会逃避你的对立面。这可能导致无效的URL - recaptcha通常要求两个单词,而空格在URL中无效,所以我建议您这样做:
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SecretKey=&response=" . rawurlencode($captcha) . "&remoteip=" . rawurlencode($_SERVER['REMOTE_ADDR']));
您还在使用旧版本的PHPMailer;请使用弄到最新的。
https://stackoverflow.com/questions/29618372
复制