neutron/recaptcha

ReCaptcha 客户端

0.1.3 2013-02-14 13:42 UTC

This package is auto-updated.

Last update: 2024-08-25 06:27:59 UTC


README

Build Status

这是原始 ReCaptcha 库的面向对象 PHP 版本。

它被设计为可测试的,并使用 Guzzle 作为传输层。

安装

使用 ReCaptcha 的推荐方法是 通过 composer

{
    "require": {
        "neutron/recaptcha": "~0.1.0"
    }
}

Silex 服务提供者

一个简单的 Silex 服务提供者

use Neutron\ReCaptcha\ReCaptcha;
use Neutron\ReCaptcha\ReCaptchaServiceProvider;
use Silex\Application;

$app = new Application();
$app->register(new ReCaptchaServiceProvider(), array(
    'recaptcha.public-key'  => 'fdspoksqdpofdkpopgqpdskofpkosd',
    'recaptcha.private-key' => 'lsdmkzfqposfomkcqdsofmsdkfkqsdmfmqsdm',
));

// $captcha is an instance of Neutron\ReCaptcha\Response
$captcha = $app['recaptcha']->bind($app['request']);

if ($captcha->isValid()) {
    echo "YEAH !";
} else {
    echo "Too bad dude :( " . $captcha->getError();
}

使用示例

向客户端显示验证码

  • 初始化您的验证码对象
use Neutron\ReCaptcha\ReCaptcha;

$recaptcha = ReCaptcha::create($publicKey, $privateKey);
  • 在您的模板中
<script type="text/javascript">
    var RecaptchaOptions = {
       theme : 'custom',
       custom_theme_widget: 'recaptcha_widget'
    };
</script>
<form method="post">
    <div id="recaptcha_widget" style="display:none">
        <div id="recaptcha_image"></div>
        <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>

        <span class="recaptcha_only_if_image">Enter the words above:</span>
        <span class="recaptcha_only_if_audio">Enter the numbers you hear:</span>

        <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />

        <div><a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a></div>
        <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">Get an audio CAPTCHA</a></div>
        <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>

        <div><a href="javascript:Recaptcha.showhelp()">Help</a></div>
    </div>

    <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=<?=$recaptcha->getPublicKey();?>"></script>
    <noscript>
       <iframe src="http://www.google.com/recaptcha/api/noscript?k=<?=$recaptcha->getPublicKey();?>" height="300" width="500" frameborder="0"></iframe><br>
       <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
       <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
    </noscript>
    <button type="submit">Submit</button>
</form>
  • 服务器端
use Neutron\ReCaptcha\ReCaptcha;

$recaptcha = ReCaptcha::create($publicKey, $privateKey);
$response = $recaptcha->checkAnswer($_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

if ($response->isValid()) {
    echo "YEAH !";
} else {
    echo "Too bad dude :(";
}

绑定 Symfony 请求

存在一个快捷方式可以将 Symfony 请求 绑定到 ReCaptcha

use Neutron\ReCaptcha\ReCaptcha;
use Symfony\Component\HttpFoundation\Request;

$recaptcha = ReCaptcha::create($publicKey, $privateKey);
$response = $recaptcha->bind(Request::createFromGlobals());

if ($response->isValid()) {
    echo "YEAH !";
} else {
    echo "Too bad dude :( " . $response->getError();
}

##许可证

本项目采用 MIT 许可证