我正在使用新版本的Recaptcha - 具有复选框而不是文本字段的版本。
但是它不起作用。尽管出现了新的 Recaptcha,但我每次选中该框时都会收到错误页面。
我已经浏览了文档,它没有提到与设置它的旧文档有任何不同之处。
我下载了最新的recaptchalib.php
,在我的标题中添加了<script src='https://www.google.com/recaptcha/api.js'></script>
,并设置了我的服务器端脚本:
require_once('recaptchalib.php');
$privatekey = "myprivatekey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
//CAPTCHA was entered incorrectly
header( "Location: $error_page" );
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
//Successful verification
我的客户端似乎很好,简单地说:
<div class="g-recaptcha" data-sitekey="mypublickey"></div>
当然,我已经仔细检查了这两个键,我所有的路径都是正确的。
是否有我要使用的新库或新复选框类型 Recaptcha 的不同处理方式?我做错了什么?
您是否绝对确定您包含一个使用新 API 的 recaptchalib.php?你能仔细检查一下吗?
我问这个是因为我看到最新版本的 recaptcha-php 是 1.11,它是在 2010 年发布的,而且我在他们的 VCS 中也没有看到任何更新。
该库使用的是旧 API:
$response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/verify",
array (
'privatekey' => $privkey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response
) + $extra_params
);
新的 API 应该像这样调用:
https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address
登录表单的简单实现可能如下所示:
<form role="form" method="POST" action="/login_check.php">
<input type="text" class="" id="username" placeholder="Enter username">
<input type="password" class="" id="password">
<div class="g-recaptcha" data-sitekey="[YOUR SITE KEY]"></div>
</form>
<script src='https://www.google.com/recaptcha/api.js'></script>
login_check.php:
<?php
// Get yours from https://www.google.com/recaptcha/intro/index.html
$api_secret = 'YOUR API SECRET';
$api_endpoint = 'https://www.google.com/recaptcha/api/siteverify';
if ((strtoupper($_SERVER["REQUEST_METHOD"])) === "POST") {
if (empty($_POST['g-recaptcha-response'])) {
// The user did not complete reCAPTCHA
header("location: index.php");
exit;
}
$api_url = sprintf('%s?secret=%s&response=%s&remoteip=%s', $api_endpoint, $api_secret, $_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
$response = json_decode(apiVerify($api_url), true);
// reCAPTCHA is NOT correct.
if (false === $response['success']) {
// The user is not a human.
header("location: index.php");
exit;
}
// reCAPTCHA was correct, so you can go ahead and check login credentials ...
echo "Welcome !";
}
function apiVerify($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");
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
您可以(并且应该)扩展此示例以向用户显示一些反馈,执行一些额外的验证等,但我认为它应该足以让您入门。
是的,"最新"recaptchalib.php
已经多年没有更新了。请务必给 Google 文档 1 星评级,他们可能会对此有所作为。我在这个问题上兜兜转转,直到我意识到recaptchalib.php
文件是问题所在。
这篇文章解释了如何使用这个 recaptchalib.php事不宜迟。
正如您已经说过的,在您的页面上,您需要:
<script src='https://www.google.com/recaptcha/api.js'></script>
<form method="POST" action="/your-action-page.php">
<!-- All your form fields here -->
<div class="g-recaptcha" data-sitekey="yourpublickey"></div>
<input type="submit" value="Submit" />
</form>
然后在您的操作页面中.php您需要以下内容:
<?php
require_once('path-to/recaptchalib.php');
$secret = "yoursecretkey";
$response = null;
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) {
$response = $reCaptcha->verifyResponse( $_SERVER["REMOTE_ADDR"], $_POST["g-recaptcha-response"] );
}
if ($response != null && $response->success) {
// Your code to handle a successful verification
}
else {
// What happens when the CAPTCHA is entered incorrectly
}
?>
并且不要忘记上传替代recaptchalib.php
:
<?php
/**
* This is a PHP library that handles calling reCAPTCHA.
* - Documentation and latest version
* https://developers.google.com/recaptcha/docs/php
* - Get a reCAPTCHA API Key
* https://www.google.com/recaptcha/admin/create
* - Discussion group
* http://groups.google.com/group/recaptcha
*
* @copyright Copyright (c) 2014, Google Inc.
* @link http://www.google.com/recaptcha
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* A ReCaptchaResponse is returned from checkAnswer().
*/
class ReCaptchaResponse {
public $success;
public $errorCodes;
}
class ReCaptcha {
private static $_signupUrl = "https://www.google.com/recaptcha/admin";
private static $_siteVerifyUrl =
"https://www.google.com/recaptcha/api/siteverify?";
private $_secret;
private static $_version = "php_1.0";
/**
* Constructor.
*
* @param string $secret shared secret between site and ReCAPTCHA server.
*/
function ReCaptcha($secret) {
if ($secret == null || $secret == "") {
die("To use reCAPTCHA you must get an API key from <a href='"
. self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
}
$this->_secret=$secret;
}
/**
* Encodes the given data into a query string format.
*
* @param array $data array of string elements to be encoded.
*
* @return string - encoded request.
*/
private function _encodeQS($data) {
$req = "";
foreach ($data as $key => $value) {
$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
}
// Cut the last '&'
$req=substr($req, 0, strlen($req)-1);
return $req;
}
/**
* Submits an HTTP GET to a reCAPTCHA server.
*
* @param string $path url path to recaptcha server.
* @param array $data array of parameters to be sent.
*
* @return array response
*/
private function _submitHTTPGet($path, $data) {
$req = $this->_encodeQS($data);
$response = file_get_contents($path . $req);
return $response;
}
/**
* Calls the reCAPTCHA siteverify API to verify whether the user passes
* CAPTCHA test.
*
* @param string $remoteIp IP address of end user.
* @param string $response response string from recaptcha verification.
*
* @return ReCaptchaResponse
*/
public function verifyResponse($remoteIp, $response) {
// Discard empty solution submissions
if ($response == null || strlen($response) == 0) {
$recaptchaResponse = new ReCaptchaResponse();
$recaptchaResponse->success = false;
$recaptchaResponse->errorCodes = 'missing-input';
return $recaptchaResponse;
}
$getResponse = $this->_submitHttpGet(
self::$_siteVerifyUrl,
array (
'secret' => $this->_secret,
'remoteip' => $remoteIp,
'v' => self::$_version,
'response' => $response
)
);
$answers = json_decode($getResponse, true);
$recaptchaResponse = new ReCaptchaResponse();
if (trim($answers ['success']) == true) {
$recaptchaResponse->success = true;
}
else {
$recaptchaResponse->success = false;
$recaptchaResponse->errorCodes = $answers [error-codes];
}
return $recaptchaResponse;
}
}
?>
希望对您有所帮助! :)