neutron/recaptcha

ReCaptcha Client

0.1.3 2013-02-14 13:42 UTC

This package is auto-updated.

Last update: 2021-09-24 23:49:28 UTC


README

Build Status

This is a Object Oriented PHP port of the original ReCaptcha lib.

It's been designed to be testable and uses Guzzle as a transport layer.

Install

The recommended way to use ReCaptcha is through composer.

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

Silex Service Provider

A simple Silex service provider :

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();
}

Usage Example

To display a captcha to the client :

  • Initialize your captcha object :
use Neutron\ReCaptcha\ReCaptcha;

$recaptcha = ReCaptcha::create($publicKey, $privateKey);
  • In your template :
<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>
  • Server side :
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 :(";
}

Bind Symfony Request

A shortcut exists to bind a Symfony Request to 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();
}

##License

This project is licensed under the MIT license.