我有一个问题,Recaptcha V2.0总是返回false或bool(false)。感觉它没有通过正确的密钥进行验证,但我检查了我添加的域名以及我在代码中使用的密钥。也许我刚刚做了一些错误的代码。
HTML表单:
<form method="post" action="/php/emailCode.php">
Your Name <label><input type="text" name="name"></label>
<br/>
Email Address <label><input type="text" name="email"></label>
<br/>
Message <label><textarea name="message"></textarea></label>
<br />
<div id="captcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxx"></div>
<br />
<input id="submitButton" type="submit">
</form>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
PHP:
<?php
if(isset($_POST['g-recaptcha-response']) && $_POST['g-recaptcha-response']){
$secret = "xxxxxxxxxxxxxxxxxx";
$ip = $_SERVER['REMOTE_ADDR'];
$captcha = $_POST['g-recaptcha-response'];
$rsp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $ip);
var_dump($rsp);
$arr = json_decode($rsp);
if($arr->success === true){
$EmailFrom = "example@example.com";
$EmailTo = "example@gmail.com";
$Subject = $_POST['email'];
$Name = $_POST['name'];
$Message = $_POST['message'];
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}else{
echo 'Failure';
}
}
同样在头脑中我运行:
<script type="text/javascript">
var onloadCallback = function() {
grecaptcha.render('captcha', {
'sitekey' : 'xxxxxxxxxxxxxxxxxxxxxx'
});
};
</script>
我完全不知道为什么它会返回false。 感谢您的帮助!
答案 0 :(得分:0)
在recaptcha中你有两把钥匙:
网站密钥 在您的网站为用户提供的HTML代码中使用此功能。
和
密钥 用于在您的网站和Google之间进行通信。一定要保密。
转到https://www.google.com/recaptcha/admin#list 并单击您的网站 并阅读“ 将reCAPTCHA添加到您的网站 ”
编辑: 在你的客户方:
.
..
...
<div id="captcha" data-sitekey="SITE KEY"></div>
...
..
.
在您的服务器端:
.
..
...
$secret = "SECRET KEY";
...
..
.
答案 1 :(得分:0)
<?php include_once "recaptchalib.php"; ?> //library to be added
<form action='' method=''>
<input type="text" id="myphn" placeholder="Email/Username">
<input type="Password" id="mypass" placeholder="Password">
<div class="g-recaptcha" data-sitekey="6Ldd7AoUAAAAAGMNn1YkptZO7s9TY2xHe7nW45ma" id='recaptcha'></div>
//contains the key
<input type='submit'>
</form>
$response=$_POST['g-recaptcha-response']
$secret = '6Ldd7AoUAAAAAGpSbJYqM9Tsh04NG_1vCPeRlFOe';
$rsp=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response");
$arr= json_decode($rsp,TRUE);
if($arr['success'] == TRUE){
//var_dump($rsp); die;
}else{ echo 'not done';
}