PHPackages                             recaptcha-lib/recaptcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. recaptcha-lib/recaptcha

ActiveLibrary[Security](/categories/security)

recaptcha-lib/recaptcha
=======================

PHP Library for reCAPTCHA Google's API

v0.1.5(7y ago)458.8k↓16.7%31MITPHPPHP &gt;=5.3.0

Since Jun 10Pushed 7y ago1 watchersCompare

[ Source](https://github.com/adrianorsouza/reCAPTCHA-lib)[ Packagist](https://packagist.org/packages/recaptcha-lib/recaptcha)[ Docs](https://github.com/adrianorsouza/reCAPTCHA-lib)[ RSS](/packages/recaptcha-lib-recaptcha/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (7)Used By (1)

PHP Library for reCAPTCHA Google's API
======================================

[](#php-library-for-recaptcha-googles-api)

Build status: [![Build Status](https://camo.githubusercontent.com/a3b392568b9f8d6986fade1cbb941113377f7872d8842f6b866745260773d0bd/68747470733a2f2f7472617669732d63692e6f72672f61647269616e6f72736f757a612f7265434150544348412d6c69622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/adrianorsouza/reCAPTCHA-lib)[![Latest Stable Version](https://camo.githubusercontent.com/c2ffda726a7c5dd9c8c3f5c220f09ccbdba4c383e3d7b4fcfd1ac122cf9cb247/68747470733a2f2f706f7365722e707567782e6f72672f7265636170746368612d6c69622f7265636170746368612f762f737461626c652e737667)](https://packagist.org/packages/recaptcha-lib/recaptcha)[![Total Downloads](https://camo.githubusercontent.com/5ab756e8b75b2bc100f11f6b3d0eb87c71c052a8b897eb4916db7b336334a3da/68747470733a2f2f706f7365722e707567782e6f72672f7265636170746368612d6c69622f7265636170746368612f646f776e6c6f6164732e737667)](https://packagist.org/packages/recaptcha-lib/recaptcha)[![Latest Unstable Version](https://camo.githubusercontent.com/73d4c6290ce1cc75084050eba8cdea30d5664c6fe222217066ab4f9debe14141/68747470733a2f2f706f7365722e707567782e6f72672f7265636170746368612d6c69622f7265636170746368612f762f756e737461626c652e737667)](https://packagist.org/packages/recaptcha-lib/recaptcha)

This is PHP library that handle Google's reCAPTCHA API, you can easy implement it into your site or app and be able to set custom configurations and options to display the widget as you want.

Library Features
----------------

[](#library-features)

- Display a reCAPTCHA V1 widget onto your form.
- Validate a given input value for an image Captcha and returns the result.
- Support to different Standard Theme, Custom Theming and Template customization.
- Internationalization support and custom language strings translation.
- Custom Global configuration if you need to run reCAPTCHA in many forms.
- Individually configuration for each widget.
- Timeout configuration for request server API.
- Error handling.
- Uses socket connection for API calls fallback to CURL whether the function `fsockopen` is not available.
- Fully customization and optimization to display reCAPTCHA
- Ease implementation

Quick Installation via composer
-------------------------------

[](#quick-installation-via-composer)

You can find this library at [Composer/Packagist](https://packagist.org/packages/recaptcha-lib/recaptcha).

```
$ composer require "recaptcha-lib/recaptcha:0.1.*"

```

### Optional Manual Install

[](#optional-manual-install)

If you don't use composer you'll have to download the latest [release](https://github.com/adrianorsouza/reCAPTCHA-lib/releases) of this library and unpack it within your project folder.

Once you've done it, you'll need only those files within the `ReCaptcha` folder *you can get rid of others files and samples in there, if you want to*.

The reCAPTCHA Libray comes with an [Autoloader](https://github.com/adrianorsouza/reCAPTCHA-lib/blob/master/lib/ReCaptcha/CaptchaAutoloader.php) that you can include in the top of your page. By using the autoloader there is no need of including files manually, see an example below:

```
require_once 'ReCaptcha/CaptchaAutoloader.php';

$captcha = new \ReCaptcha\Captcha();
echo $captcha->displayHTML();
```

Displaying reCAPTCHA
--------------------

[](#displaying-recaptcha)

Display default options and theme.

```
use ReCaptcha\Captcha;

$captcha = new Captcha();
$captcha->setPublicKey('YourPublicKey');
echo $captcha->displayHTML();
```

Display a Standard Theme, your API Keys and options from a config file.

```
use ReCaptcha\Captcha;

$my_config = '/optional/path/to/captcha_config.php';
$captcha = new Captcha();
$captcha->setConfig($my_config);
echo $captcha->displayHTML();
```

To display a different theme and language straightforward.

```
echo $captcha->displayHTML('theme_name', array('lang'=>'es'));
```

Samples
-------

[](#samples)

ReCaptcha-lib provides many ways to display and configure a reCAPTCHA widget. A theme can be set by a given name e.g: `Captcha::displayHTML('clean')`. You can find all **Standard Theme** names in .

`displayHTML();``displayHTML('white');``displayHTML('clean');``displayHTML('custom');`[![image](https://camo.githubusercontent.com/c6baa7f0f91625c6b6f56ef6205e6c7ef6f82bc64c671b0c82b64cd22b5d1712/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65312e706e67)](https://camo.githubusercontent.com/c6baa7f0f91625c6b6f56ef6205e6c7ef6f82bc64c671b0c82b64cd22b5d1712/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65312e706e67)[![image](https://camo.githubusercontent.com/fd2e99bcb436438a962f82f00366bbcda7c220585f789f92be61ba56d78dcf9d/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65322e706e67)](https://camo.githubusercontent.com/fd2e99bcb436438a962f82f00366bbcda7c220585f789f92be61ba56d78dcf9d/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65322e706e67)[![image](https://camo.githubusercontent.com/13eb513f66b724bd9df74a393cea49f495a8dc5e6393bf84719856e9e13dfbb5/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65332e706e67)](https://camo.githubusercontent.com/13eb513f66b724bd9df74a393cea49f495a8dc5e6393bf84719856e9e13dfbb5/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65332e706e67)[![image](https://camo.githubusercontent.com/c1bdbed27f5add57dc1e69deab925fae716bb6a30aadfd4419be38e8846ed44e/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65342e706e67)](https://camo.githubusercontent.com/c1bdbed27f5add57dc1e69deab925fae716bb6a30aadfd4419be38e8846ed44e/687474703a2f2f61647269616e6f72736f757a612e6769746875622e696f2f7265434150544348412d6c69622f696d616765732f73616d706c65342e706e67)---

> **NOTE:** When using a `custom` theme, as you can see above, the template is displayed in pure HTML so you need to provide a CSS to display a custom theme properly to your users. The widget elements are wrapped within a `div` container default ID `recaptcha_widget` but you can change this ID using the option `custom_theme_widget`

```
array('custom_theme_widget' => 'my_widget_id_name');
```

You can find more examples how to implement reCAPTCHA in [examples](https://github.com/adrianorsouza/reCAPTCHA-lib/tree/master/examples) folder.

Verifying reCAPTCHA User's Answer
---------------------------------

[](#verifying-recaptcha-users-answer)

The Library has a method `Captcha::isValid()` to check the user's answer when performing a form post. It's easy to implement.

**Basic Verify**

```
$captcha = new Captcha();
$captcha->setPrivateKey('YourPrivateKey');

if ( !$captcha->isValid() ) {

   echo $captcha->getError();
   // stop action

} else {

   echo 'Captcha Valid!!!';
   // do something else
}
```

The method `Captcha::isValid()` takes an action only when its encountered a `REQUEST_METHOD` `POST`, so you can set in the same page that method `Captcha::displayHTML()`

```
$captcha = new Captcha();

$captcha->setPublicKey('YourPublicKey');
$captcha->setPrivateKey('YourPrivateKey');

// only perform when the form is submitted
if ( !$captcha->isValid() ) {
   echo $captcha->getError();
}

echo $captcha->displayHTML();
// something else ...
```

Options
-------

[](#options)

Custom options can be set by passing an array of values to `Captcha::displayHTML()`

```
array('lang'=>'it', 'tabindex'=>2);
```

Custom theming and language of the widget

```
$captcha->displayHTML(
	'custom',
	array(
		'lang'=>'it',
		'custom_theme_widget'=>'my_container_id'
	)
);
```

Configuration
-------------

[](#configuration)

#### Changing Widget Language

[](#changing-widget-language)

Set a language in object constructor as a first parameter.

```
$captcha = new Captcha('pt');
```

#### Run reCAPTCHA Over SSL

[](#run-recaptcha-over-ssl)

When using SSL site, to avoid getting browser certificate warnings when you display reCAPTCHA set a second parameter to `TRUE` in object constructor.

```
$captcha = new Captcha(NULL, TRUE);
```

`NULL` set the language to default system built-in translation. `TRUE` enable call API using https://

#### Timeout

[](#timeout)

The Library has a timeout when performing a request server API to verify the user's answer. The default timeout value is 10. If you want to change.

```
$captcha->timeout = 100;
```

#### Global Config

[](#global-config)

If you want instead of passing arrays all the time into your widget you might set a [Global configuration](https://github.com/adrianorsouza/reCAPTCHA-lib/blob/master/lib/ReCaptcha/captcha_config.php) by creating an external file with all options and configurations to be read globally.

Sample File: `captcha_config.php`.

```
// your KEYs
$CAPTCHA_CONFIG['publicKey']  = '6Ldoa_YourPublicKey';
$CAPTCHA_CONFIG['privateKey'] = '6Ldoa_YourPrivateKey';

// reCAPTCHA options
$CAPTCHA_CONFIG['recaptchaOptions'] = array(
                     'theme'    => 'custom',
                     'lang'     => 'it',
                     'tabindex' => 0
                     );

// Enable/Disable when you use reCAPTCHA on SSL site
$CAPTCHA_CONFIG['ssl'] = false;

// Set Server API timeout
$CAPTCHA_CONFIG['timeout'] = 10;
```

Then pass its path to the method: `Captcha::setConfig($path)`.

```
// Set Global config captcha_config.php
$my_config = '/path/to/my/captcha_config.php';
$captcha = new Captcha();
$captcha->setConfig($my_config);
```

Internationalization
--------------------

[](#internationalization)

This library has two different approaches to display reCAPTCHA widget in a different language to the user's screen.

#### Built-in Language

[](#built-in-language)

It's a default Google's reCAPTCHA API language translation, so the widget strings are shown according the user's browser language preference, whether user's language is not in the system built-in the English will be used instead.

#### Custom language

[](#custom-language)

If you are using `Custom Theme Template` you must set a customized translation as well, despite using any **Standard Theme** that you need to display your reCAPTCHA widget in a different language which is not available in system built-in you can use your own translation strings.

This library offers you a simple way to customize translation strings, within a folder [`I18n`](https://github.com/adrianorsouza/reCAPTCHA-lib/tree/master/lib/ReCaptcha/I18n) there's a few languages already translated in there. If you want to use one of them just pass the language code as first parameter to the object constructor. See examples in section [options](#options) and [configuration](#configuration) above.

Even if your language is not in there, you have two options to get your widget translated.

##### Create a Language File

[](#create-a-language-file)

You might translate the widget strings by writing a file `recaptcha.lang.[lang_code].php` *take a look one of them within `I18n` folder* as a reference\*, place it somewhere in your `dir`, then tell to the library where it is by passing its path as a second parameter to the method `Captcha::setTranslation(lang_code, path)`.

```
$myCustomLang = '/optional/path/to/file/lang'; // no need trailing slash.
$captcha->setTranslation('fr', $myCustomLang);
```

This will point to a file: `/optional/path/to/file/lang/recaptcha.lang.fr.php`

> **NOTE:** your custom Lang file must be named as `recaptcha.lang.[lang_code].php` where `lang_code` is your language abbreviation e.g. 'French' is `fr`.

##### Optional

[](#optional)

Instead of create an external Lang file you may also translate your widget strings by passing an array to the `custom_translations` option:

```
$my_options = array(
   'custom_theme_widget'=>'recaptcha_widget',
   'tabindex' => 0,
   'lang' => 'de',
   'custom_translations' => array(
      'instructions_visual' => 'Geben Sie den angezeigten Text ein',
      'instructions_audio'  => 'Geben Sie das Gehörte ein:',
      'play_again'          => 'Wort erneut abspielen   ',
      'cant_hear_this'      => 'Wort als MP3 herunterladen',
      'visual_challenge'    => 'Captcha abrufen',
      'audio_challenge'     => 'Audio-Captcha abrufen',
      'refresh_btn'         => 'Neues Captcha abrufen',
      'help_btn'            => 'Hilfe',
      'incorrect_try_again' => 'Falsch, bitte versuchen Sie es erneut.'
    )
);
```

**Collaborate**If you want to translate a language file and collaborate, fork this project do it and send us a pull request.

Theme Customization
-------------------

[](#theme-customization)

There are a few **Standard Theme** ready to go with, as written in [Samples](#samples), but to go any further, it's possible to style as you want by adding an option `theme=>custom` as array to config file, by parameter option or straightforward `Captcha::displayHTML('custom')`. To do so set one of these options and create a CSS to prettify it, you can checkout [demo](http://www.google.com/recaptcha/demo/custom) or see more in reCAPTCHA [Custom Theming](https://developers.google.com/recaptcha/docs/customization#Custom_Theming) Docs.

Documentation
-------------

[](#documentation)

This library has a better documentation generated by PHPDoc you can take a look at [Documentation Page](http://adrianorsouza.github.io/reCAPTCHA-lib/).

Bugs
----

[](#bugs)

Have you found a bug? Please open a new [issue](https://github.com/adrianorsouza/reCAPTCHA-lib/issues).

### Author

[](#author)

Adriano Rosa [@adrianorosa](https://twitter.com/adrianorosa)

### Notes

[](#notes)

In order to start using this library you must generate your reCAPTCHA's API Key from [Google's reCAPTCHA](https://www.google.com/recaptcha/admin) site.

### License

[](#license)

This software is licensed under the MIT License. Please read LICENSE for information on the software availability and distribution.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~304 days

Recently: every ~336 days

Total

6

Last Release

2839d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a97d7e781ceb36f47a31b7860870278b88bbcf99d4f63ddec55b945dcf6e2a2?d=identicon)[adrianorosa](/maintainers/adrianorosa)

---

Top Contributors

[![adrianorsouza](https://avatars.githubusercontent.com/u/5430240?v=4)](https://github.com/adrianorsouza "adrianorsouza (89 commits)")

---

Tags

captchadisplay-recaptchagoogle-recaptchaphpphp-captcharecaptcha-widgetrecaptchacaptcharecapcha-lib

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/recaptcha-lib-recaptcha/health.svg)

```
[![Health](https://phpackages.com/badges/recaptcha-lib-recaptcha/health.svg)](https://phpackages.com/packages/recaptcha-lib-recaptcha)
```

###  Alternatives

[phelium/recaptcha

reCAPTCHA v2 class

4389.4k](/packages/phelium-recaptcha)[fruitcakestudio/recaptcha

reCAPTCHA library

1625.5k1](/packages/fruitcakestudio-recaptcha)[cakephp-fr/recaptcha

To easily use Google Recaptcha (free CAPTCHA service that protect websites from spam and abuse) in CakePHP projects

1419.3k](/packages/cakephp-fr-recaptcha)[cake17/cakephp-recaptcha

\[DEPRECIATED\] Please use https://github.com/cakephp-fr/recaptcha/

1211.0k1](/packages/cake17-cakephp-recaptcha)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
