reCAPTCHA
reCAPTCHA – getting started with the reCAPTCHA functionality of Services_ReCaptcha
Instanciating the Services_ReCaptcha class
To instanciate the Services_ReCaptcha class just do:
<?php
require_once 'Services/ReCaptcha.php';
$recaptcha = new Services_ReCaptcha('your_public_key', 'your_private_key');
?>
You can also pass an array of option as third parameter, or pass options
later with the
Services_ReCaptcha_Base::setOption()
or
Services_ReCaptcha_Base::setOptions()
Available options are:
Services_ReCaptcha options
Name |
Description |
Type |
Default value |
secure |
Whether to force the ssl url or not |
boolean |
false |
xhtml |
Whether the html should be xhtml compliant or not |
boolean |
true |
theme |
The theme to use for the CAPTCHA |
string |
red |
lang |
The language to use for the CAPTCHA (must be one of the reCATCHA supported languages codes) |
string |
en |
custom_translations |
An array of cutom translations to use |
array |
null |
custom_theme_widget |
The id of the HTML element corresponding to the theme widget |
string |
null |
tabindex |
The HTML tabindex attribute for the reCAPTCHA textarea |
string |
null |
For more information about these options please consult
relevant reCAPTCHA API docs.
A simple recaptcha example
<?php
/**
* Include the Services_ReCaptcha class
*/
require_once 'Services/ReCaptcha.php';
// you must get your API keys here:
// http://recaptcha.net/api/getkey
$publicKey = 'your_public_key';
$privateKey = 'your_private_key';
// we instanciate our Services_ReCaptcha instance with the public key and the
// private key
$recaptcha = new Services_ReCaptcha($publicKey, $privateKey);
// if the form was submitted and the catpcha challenge response is ok, we
// display a message and exit
if (isset($_POST['submit']) && $recaptcha->validate()) {
echo "Challenge response ok !";
exit(0);
}
// we display the html form
?>
<html>
<head>
<title>recaptcha test</title>
</head>
<body>
<form method="post" action="">
<?php echo $recaptcha; ?>
<hr/>
<input type="submit" name="submit" value="Ok"/>
</form>
</body>
</html>
A more advanced recaptcha example
<?php
/**
* Include the Services_ReCaptcha class
*/
require_once 'Services/ReCaptcha.php';
// you must get your API keys here:
// http://recaptcha.net/api/getkey
$publicKey = 'your_public_key';
$privateKey = 'your_private_key';
// we instanciate our Services_ReCaptcha instance with the public key and the
// private key
$recaptcha = new Services_ReCaptcha($publicKey, $privateKey);
// we are going to customize our Services_ReCaptcha instance
$recaptcha->setOption('secure', true); // we force the secure url
$recaptcha->setOption('theme', 'white'); // use the white theme
$recaptcha->setOption('lang', 'fr'); // set language to french
// alternatively we could have done:
// $recaptcha = new Services_ReCaptcha($publicKey, $privateKey, array(
// 'secure' => true,
// 'theme' => 'white',
// 'lang' => 'fr'
// ));
// or:
// $recaptcha->setOptions(array('theme' => 'white', 'lang' => 'fr'));
// we use a proxy, so we need to configure it
$recaptcha->getRequest()->setConfig(
array('proxy_host' => 'localhost', 'proxy_port' => 8118)
);
// if the form was submitted
if (isset($_POST['submit'])) {
if ($recaptcha->validate()) {
// the catpcha challenge response is ok, we display a message and exit
echo "Challenge response ok !";
exit(0);
} else {
// if the captcha validation failed, instead of letting the captcha
// display the error, we want to echo the error and exit
echo $recaptcha->getError();
exit(1);
}
}
// we display the html form
?>
<html>
<head>
<title>recaptcha test</title>
</head>
<body>
<form method="post" action="">
<?php echo $recaptcha; ?>
<hr/>
<input type="submit" name="submit" value="Ok"/>
</form>
</body>
</html>