我下载了一个recaptchalib并成功地实现了recaptcha并在我的页面上显示了它,但是我无法验证它.如何验证recaptcha
视图文件
<div id="recaptcha_div"></div>
<script type="text/javascript">
$(function(){
Recaptcha.create("<?php echo Configure::read("recatpch_settings.public_key")?>", 'recaptcha_div', {
theme: "red",
callback: Recaptcha.focus_response_field});
});
</script>
控制器登录动作
public function login() {
App::import('Vendor', 'recaptchalib', array('file' => 'recaptchalib/recaptchalib.php'));
$resp = recaptcha_check_answer (Configure::read("recatpch_settings.private_key"),
$_SERVER["REMOTE_ADDR"],
$this->params['form']["recaptcha_challenge_field"],
$this->params['form']["recaptcha_response_field"]);
pr($resp);
exit();
if (!$resp->is_valid) {
$this->Session->setFlash('The reCAPTCHA wasn\'t entered correctly. Please, try again.');
} else {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Your username/password combination was incorrect');
}
}
}
}
我无法验证验证码。如果我输入正确的captcha并明显地输入用户名和密码,我想登录用户。
发布于 2015-04-24 21:35:04
我知道这是一个很老的问题,但我将继续回答那些可能正在研究实现这个问题的人。
将站点/秘密密钥添加到应用程序/config/bootstrap.php文件.
//Recaptcha Config
Configure::write('Recaptcha.SiteKey','YourSiteKey');
Configure::write('Recaptcha.SecretKey','YourSecretKey');
将reCaptcha小部件添加到视图/表单中:
<div>
<div class="g-recaptcha"
data-sitekey="<?php echo Configure::read('Recaptcha.SiteKey'); ?>">
</div>
<?php echo $this->Html->script('https://www.google.com/recaptcha/api.js"'); ?>
</div>
验证用户的响应(控制器中的可重用函数):
private function __checkRecaptchaResponse($response){
// verifying the response is done through a request to this URL
$url = 'https://www.google.com/recaptcha/api/siteverify';
// The API request has three parameters (last one is optional)
$data = array('secret' => Configure::read('Recaptcha.SecretKey'),
'response' => $response,
'remoteip' => $_SERVER['REMOTE_ADDR']);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
// We could also use curl to send the API request
$context = stream_context_create($options);
$json_result = file_get_contents($url, false, $context);
$result = json_decode($json_result);
return $result->success;
}
在提交包含小部件:的表单后调用上面的函数来检查响应
if($this->__checkRecaptchaResponse($this->request->data['g-recaptcha-response'])){
// user solved the captcha
} else {
// user failed to solve the captcha
}
有用资源:
发布于 2017-08-26 08:21:48
谷歌已经推出了新的reCaptcha API,你是机器人吗?一种新的设计captcha系统。这为机器人和垃圾邮件发送者保护您的网站,在这篇文章中,我使用reCaptch登录表单使用CakePHP实现了新的HTML系统。请快速看一看演示。
Get reCaptcha Key
单击此处来创建Google reCaptcha应用程序。
注册您的网站
请提供您的网站域名详细信息,而不需要http:
Google网站密钥
您将在HTML代码中使用此方法。
Google密钥
这将有助于您的网站与谷歌沟通。
HTML代码
包含带有Google reCaptcha代码段的HTML代码。您必须修改GOOGLE_SITE_KEY值。
<html>
<head>
/* Google reCaptcha JS */
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="" method="post">
<label>Username</label>
<?php echo $this->Form->text('User.username', array('maxlength' => 32))?>
<label>Password</label>
<?php echo $this->Form->password('User.password', array('maxlength' => 32))?>
<div class="g-recaptcha" data-sitekey="GOOGLE_SITE_KEY"></div>
<input type="submit" value="Log In" />
</form>
</body>
</html>
在供应商文件中创建新的供应商:
curl.php
<?php
function getCurlData($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>
在控制器中使用时,必须修改GOOGLE_SECRET_KEY值。
$recaptcha = $this->data['g-recaptcha-response'];
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret = 'GOOGLE_SECRET_KEY';
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url . "?secret=" . $secret . "&response=" . $recaptcha ."&remoteip=" . $ip;
App::import('Vendor', 'curl');
$res = getCurlData($url);
$res = json_decode($res, true);
if(empty($res['success'])){
//if success not empty
//some code here
}
希望它有用。
https://stackoverflow.com/questions/18279515
复制相似问题