我在一天中的大部分时间里都尽力使它正常工作,但是似乎所有人都无法完全使用我的代码。我有一个工作正常的sendmail脚本,只想添加google reCaptcha。到目前为止,除非选中复选框,否则它不会发送联系表,但是如果未选中,则我的字段验证器将不起作用,并且不会提示用户检查reCaptcha。但是,如果所有字段均已填写并且勾选了复选框,它会回显成功消息。
我已经在Google上尝试了很多tut,并尝试遵循google开发人员文档,但是似乎无法正确解码我的回答并正确获取if,else语句!
//Checking For reCAPTCHA
$captcha;
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response =
file_get_contents("https://www.google.com/recaptcha/api/siteverify?
secret=SECRETKEY&response=" . $captcha . "&remoteip=" .
$_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
echo "Your CAPTCHA response was wrong.";
exit ;
} else {
session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );
$to = 'hi@shanemuirhead.co.uk'; // put your email here
$email_template = 'simple.html';
$subject = strip_tags($_POST['vsubject']);
$email = strip_tags($_POST['vemail']);
$name = strip_tags($_POST['vname']);
$message = nl2br( htmlspecialchars($_POST['vmessage'], ENT_QUOTES) );
$result = array();
if(empty($name)){
$result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong> Name is empty.' );
echo json_encode($result );
die;
}
if(empty($email)){
$result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong> Email is empty.' );
echo json_encode($result );
die;
}
if(empty($message)){
$result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong> Message body is empty.' );
echo json_encode($result );
die;
}
$headers = "From: " . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$templateTags = array(
'{{subject}}' => $subject,
'{{email}}'=>$email,
'{{message}}'=>$message,
'{{name}}'=>$name
);
$templateContents = file_get_contents( dirname(__FILE__) . '/inc/'.$email_template);
$contents = strtr($templateContents, $templateTags);
if ( mail( $to, $subject, $contents, $headers ) ) {
$result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong> Your email has been delivered.' );
} else {
$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong> Cann\'t Send Mail.' );
}
echo json_encode( $result );
die;
}
我只想提醒用户是否reCatpcha未打钩,如果已发送,则也可以使我的所有sendmail.php正常工作。也就是说,如果姓名,电子邮件或消息为空,则回显一条消息,好像它们被错过了?
答案 0 :(得分:0)
$response =
file_get_contents("https://www.google.com/recaptcha/api/siteverify?
secret=SECRETKEY&response=" . $captcha . "&remoteip=" .
$_SERVER['REMOTE_ADDR']);
$s = json_decode($response);
if (!$captcha || $s.success == false) {
echo "Your CAPTCHA response was wrong.";
exit ;
尝试一下,可能直接访问此$ response无效,您是否尝试过记录调试器中的$ response是什么?
答案 1 :(得分:0)
<?php
session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );
$to = 'hi@shanemuirhead.co.uk'; // put your email here
$email_template = 'simple.html';
$subject = strip_tags($_POST['vsubject']);
$email = strip_tags($_POST['vemail']);
$name = strip_tags($_POST['vname']);
$message = nl2br( htmlspecialchars($_POST['vmessage'], ENT_QUOTES) );
$result = array();
if(empty($email)){
$result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong> Email is empty.' );
echo json_encode($result );
die;
}
if(empty($name)){
$result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong> Name is empty.' );
echo json_encode($result );
die;
}
if(empty($message)){
$result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong> Message body is empty.' );
echo json_encode($result );
die;
}
if(empty($_POST['g-recaptcha-response'])) {
$result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong> Please complete the Captcha.' );
echo json_encode($result );
die;
}
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
$secret = '6LeSSI0UAAAAAFnwmu5cj7bk4guiaoXXnlID5-C2';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success == true) {
$headers = "From: " . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$templateTags = array(
'{{subject}}' => $subject,
'{{email}}'=>$email,
'{{message}}'=>$message,
'{{name}}'=>$name
);
$templateContents = file_get_contents( dirname(__FILE__) . '/inc/'.$email_template);
$contents = strtr($templateContents, $templateTags);
if ( mail( $to, $subject, $contents, $headers ) ) {
$result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong> Your email has been delivered.' );
} else {
$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong> Can\'t Send Mail.' );
}
echo json_encode( $result );
die;
}
else
{
echo "<p> Sorry verification valid</p>";
}
}
?>
因此,经过一些操作,然后将代码读回给自己,除了不确定如何重置为验证码之外,我已经能够正常工作!
因此,最后我让所有空字段验证器都可以工作,在满足条件后,它会警告用户在验证码上打钩(如果尚未这样做),然后才发送表单提交并向用户发送成功消息警告。现在唯一的问题是重置验证码。