PHPackages                             holduix/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. holduix/recaptcha

ActiveLibrary[Security](/categories/security)

holduix/recaptcha
=================

Google reCAPTCHA integration for PHP (supports v2 and v3)

2.0.0(1y ago)05GPL-2.0-onlyPHPPHP &gt;=7.2.0

Since Jan 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Holsonmp/recaptcha)[ Packagist](https://packagist.org/packages/holduix/recaptcha)[ Docs](https://github.com/holsonmp/recaptcha)[ RSS](/packages/holduix-recaptcha/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (2)Used By (0)

reCAPTCHA
=========

[](#recaptcha)

This PHP package allows you to easily integrate Google reCAPTCHA (v2 and v3) into your projects. It supports the basic features of reCAPTCHA v2 (checkbox and invisible) as well as the new features of reCAPTCHA v3 (score-based).

- [Installation](#installation)
- [Initialization](#initialization)
- [Usage](#usage)
    - [reCAPTCHA v2](#recaptcha-v2)
    - [reCAPTCHA v3](#recaptcha-v3)
- [Customization](#customization)
    - [Theme](#theme)
    - [Language](#language)
    - [Type](#type)
    - [Size](#size)
    - [Score Threshold (v3 only)](#score-threshold-v3-only)
- [Full Examples](#full-examples)
    - [reCAPTCHA v2 Example](#recaptcha-v2-example)
    - [reCAPTCHA v3 Example](#recaptcha-v3-example)

---

Installation
------------

[](#installation)

With Composer, add this line to the *require* section of your `composer.json` file:

```
"Holduix/recaptcha": "dev-master"
```

Then run the following command:

```
composer update
```

or

```
composer require holduix/recaptcha
```

---

Initialization
--------------

[](#initialization)

To initialize reCAPTCHA, you need to provide your public key (site key) and your secret key (secret key). You can do this in two ways:

### Method 1: Directly in the builder

[](#method-1-directly-in-the-builder)

```
require 'vendor/autoload.php';

use Holduix\Component\reCAPTCHA;

$reCAPTCHA = new reCAPTCHA('your-site-key', 'your-secret-key', 'v2'); // or 'v3' for reCAPTCHA v3
```

### Method 2: Via separate methods

[](#method-2-via-separate-methods)

```
require 'vendor/autoload.php';

use Holduix\Component\reCAPTCHA;

$reCAPTCHA = new reCAPTCHA();
$reCAPTCHA->setSiteKey('your-site-key');
$reCAPTCHA->setSecretKey('your-secret-key');
$reCAPTCHA->setVersion('v2'); // or 'v3' for reCAPTCHA v3
```

---

Usage
-----

[](#usage)

### reCAPTCHA v2

[](#recaptcha-v2)

reCAPTCHA v2 is the classic version that displays an invisible checkbox or captcha. Here's how to use it:

#### Generate the script

[](#generate-the-script)

```
echo $reCAPTCHA->getScript();
```

#### Generate HTML block

[](#generate-html-block)

```
echo $reCAPTCHA->getHtml();
```

#### Server-side validation

[](#server-side-validation)

```
if ($reCAPTCHA->isValid($_POST['g-recaptcha-response'])) {
    // Le captcha est valide
    echo "Captcha valide !";
} else {
    // Afficher les erreurs
    var_dump($reCAPTCHA->getErrorCodes());
}
```

### reCAPTCHA v3

[](#recaptcha-v3)

reCAPTCHA v3 works without user interaction and returns a score between 0.0 and 1.0. Here's how to use it:

#### Generate the script

[](#generate-the-script-1)

```
echo $reCAPTCHA->getScript();
```

#### Generate hidden field

[](#generate-hidden-field)

```
echo $reCAPTCHA->getHtml();
```

#### Server-side validation

[](#server-side-validation-1)

```
if ($reCAPTCHA->isValid($_POST['g-recaptcha-response'])) {
    // Le captcha est valide
    echo "Captcha valide !";
} else {
    // Afficher les erreurs
    var_dump($reCAPTCHA->getErrorCodes());
}
```

---

Customization
-------------

[](#customization)

### Theme

[](#theme)

Several themes are available for reCAPTCHA v2: `light` (default) or `dark`.

```
$reCAPTCHA->setTheme('dark');
```

### Language

[](#language)

You can change the language of reCAPTCHA. By default, the language is automatically detected.

```
$reCAPTCHA->setLanguage('fr'); // Français
```

### Type

[](#type)

For reCAPTCHA v2 you can choose between `image` (default) or `audio`.

```
$reCAPTCHA->setType('audio');
```

### Size

[](#size)

For reCAPTCHA v2 you can choose between `normal` (default) or `compact`.

```
$reCAPTCHA->setSize('compact');
```

### Score Threshold (v3 only)

[](#score-threshold-v3-only)

For reCAPTCHA v3, you can set a score threshold (between 0.0 and 1.0). By default, the threshold is set to 0.5.

```
$reCAPTCHA->setScoreThreshold(0.7); // Custom Threshold
```

---

Full Examples
-------------

[](#full-examples)

### reCAPTCHA v2 Example

[](#recaptcha-v2-example)

```
