![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
reCAPTCHA Client API DocumentationreCAPTCHA is a freely available CAPTCHA implementation. It distinguishes humans from computers. This document describes how to embed a reCAPTCHA on your website and how to customize its appearance. How the reCAPTCHA API Works
This guide focuses on the first three steps. We have a set of APIs for checking the reCAPTCHA answer. Signing up for a reCAPTCHA KeyIn order to use reCAPTCHA, you need a public/private API key pair. This key pair helps to prevent an attack where somebody hosts a reCAPTCHA on their website, collects answers from their visitors and submits the answers to your site. You can sign up for a key on the reCAPTCHA Administration Portal. Key ScopeYour reCAPTCHA token is valid only at the domain you sign up for and any subdomains (due to the potential attack mentioned above). Some users require keys for multiple sites, for example, a development server and a production server or simply multiple sites hosted on the same server. Three techniques can be used to work around this:
Getting a reCAPTCHA ChallengeThere are three ways to load the reCAPTCHA challenge on your web page
Challenge and non-JavaScript APIThe Challenge and non-JavaScript API are usually used together. To use these APIs together, you insert the following snippet of code in your website:
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=<your_public_key>">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=<your_public_key>"
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>
This code must be inserted within a <form> tag. The <noscript> code is there in case the user's browser does not support JavaScript. It embeds the two widget form fields manually. These APIs create two form fields: recaptcha_challenge_field is a hidden field that describes the CAPTCHA which is put on the page. recaptcha_response_field is a text field where the user can enter their answer. The challenge and noscript APIs take a query string parameter named "error". If an error occurred when validating a reCAPTCHA you should pass the error code in this parameter so it can be shown to the user. reCAPTCHA AJAX APIInteractive Web applications may need to dynamically create the reCAPTCHA widget in response to user input. To support such applications, we also offer an AJAX API. (Here is a simple demo of the AJAX API). To enable the AJAX API, link to the recaptcha_ajax.js file in your HTML: <script type="text/javascript" src="http://api.recaptcha.net/js/recaptcha_ajax.js"></script> This JavaScript file defines an object called Recaptcha. The methods in this object allow you to create CAPTCHA areas. The most important is the create method, which creates the CAPTCHA area:
Recaptcha.create("6LdIEwAA......",
"recaptcha_div", {
theme: "red",
callback: Recaptcha.focus_response_field
});
This creates a reCAPTCHA with the given public key in the recaptcha_div HTML element. In this example the "red" reCAPTCHA theme is used. Once the CAPTCHA area is created, the focus_response_field method will be called, focusing the reCAPTCHA area. To submit the user's CAPTCHA response to the server, use the get_challenge and get_responsedestroy after you submit a reCAPTCHA, as challenges can not be attempted multiple times. Depending on your site's requirements, you may not need all of these functions; only Recaptcha.create() is strictly required. The HTML source of the AJAX demo is a good place to get started. Look & Feel CustomizationYou can optionally customize the look of the reCAPTCHA widget with some JavaScript settings. To use these settings, insert a <script> tag that defines a dictionary called RecaptchaOptions. The following dictionary fields can be set:
As an example, this script tag makes the reCAPTCHA have a white theme and have tabindex 2:
<script>
var RecaptchaOptions = {
theme : 'white',
tabindex : 2
};
</script>
Custom themingYou can customize the look of reCAPTCHA to adapt it to your website. Custom theming allows you to control exactly how the reCAPTCHA image appears. You can see a demo of custom theming. In order to use custom theming, you must first set the theme property of the RecaptchaOptions to 'custom'. This tells reCAPTCHA that it should not create a user interface of its own. reCAPTCHA will rely on the presence of HTML elements with the following IDs to display the CAPTCHA to the user:
The Recaptcha JavaScript object provides methods which allow you to change the state of the CAPTCHA. You will want to look at the reload and switch_type methods for your theming implementation. In order to create a UI for the CAPTCHA, you need to display different information when the CAPTCHA is in different states. For example, when the user is viewing an image CAPTCHA, a "switch to image" button should not be shown. Four CSS classes are available for you to create a stateful UI.
While theming does give you many options, you need to follow some user interface consistency rules:
Methods of the Recaptcha objectThe Recaptcha JavaScript object has various methods that may be helpful. Only methods documented in this section should be relied upon. Specifically, any methods beginning with an underscore are subject to change and should not be used.
Using reCAPTCHA on an https siteIn order to avoid getting browser warnings, if you use reCAPTCHA on an SSL site, you should replace http://api.recaptcha.net with https://api-secure.recaptcha.net. For example:
<script type="text/javascript"
src="https://api-secure.recaptcha.net/challenge?k=<your_public_key>">
</script>
<noscript>
<iframe src="https://api-secure.recaptcha.net/noscript?k=<your_public_key>"
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>
Validating the CAPTCHAThis document explains about how to display the reCAPTCHA widget. To validate it, you'll need to use one of our plugins or write your own wrapper around the validation api. Tips & Guidelines
That's it. You might want to look at the resources page to see examples of how to use reCAPTCHA in applications. Other Resources
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||