![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
|
PHP Library for reCAPTCHAThe reCAPTCHA PHP Library provides a simple way to place a CAPTCHA on your PHP website, helping you stop bots from abusing your website. The library wraps the reCAPTCHA API. The library also includes an API for Mailhide, a way to wrap email addresses in a reCAPTCHA, hiding them from spammers. reCAPTCHA QuickstartThese instructions should get you started quickly.
The recaptcha_get_html functionThe recaptcha_get_html function displays that HTML that presents reCAPTCHA to the user.
Customizing client side behaviorThere are many options available to customize the look and feel of the client side code. You can even create your own theme. Look at our client API guide for more information on this topic. The recaptcha_check_answer functionAfter the user has filled out the HTML form, including their answer for the CAPTCHA, we want to check their answer when they submit the form using the recaptcha_check_answer function. The user's answer will be in two form fields, recaptcha_challenge_field and recaptcha_response_field. The reCAPTCHA library will make an HTTP request to the reCAPTCHA server and verify the user's answer.
MailhideThe reCAPTCHA PHP Library includes bindings for the Mailhide API. This API allows you to wrap an email in a reCAPTCHA to prevent spammers from seeing it: exam...@example.com. The Mailhide portion of the PHP Library requires the PHP mcrypt module. The Mailhide API consists of two functions recaptcha_mailhide_html and recaptcha_mailhide_url. The functions have the same parameters. the _html version returns HTML that can be directly put on your web page. The username portion of the email that is passed in is truncated and replaced with a link that calls Mailhide. The _url version gives you the url to decode the email and leaves it up to you to place the email in HTML.
ExamplesThe following is a "Hello World" with reCAPTCHA: <html>
<body>
<form action="" method="post">
<?php
require_once('recaptchalib.php');
$publickey = "...";
$privatekey = "...";
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
# are we submitting the page?
if ($_POST["submit"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
echo "You got it!";
# in a real application, you should send an email, create an account, etc
} else {
# set the error code so that we can display it. You could also use
# die ("reCAPTCHA failed"), but using the error message is
# more user friendly
$error = $resp->error;
}
}
echo recaptcha_get_html($publickey, $error);
?>
<br/>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
The following example shows how to use Mailhide:
<html><body>
<?
require_once ("recaptchalib.php");
// get a key at http://mailhide.recaptcha.net/apikey
$mailhide_pubkey = '';
$mailhide_privkey = '';
?>
The Mailhide version of example@example.com is
<?
echo recaptcha_mailhide_html ($mailhide_pubkey,
$mailhide_privkey,
"example@example.com");
?>.
<br>
The url for the email is:
<?
echo recaptcha_mailhide_url ($mailhide_pubkey,
$mailhide_privkey,
"example@example.com");
?>
<br>
</body></html>
If you're looking for some more examples, take a look at the WordPress and MediaWiki plugins, which use this library. Guidelines for CAPTCHA Integration:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||