holduix/recaptcha

Google reCAPTCHA integration for PHP (supports v2 and v3)


Keywords
security, recaptcha, google, captcha, v3, v2
License
GPL-2.0-only

Documentation

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

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

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

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

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

reCAPTCHA v2

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

Generate the script

echo $reCAPTCHA->getScript();

Generate HTML block

echo $reCAPTCHA->getHtml();

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 works without user interaction and returns a score between 0.0 and 1.0. Here's how to use it:

Generate the script

echo $reCAPTCHA->getScript();

Generate hidden field

echo $reCAPTCHA->getHtml();

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());
}

Customization

Theme

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

$reCAPTCHA->setTheme('dark');

Language

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

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

Type

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

$reCAPTCHA->setType('audio');

Size

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

$reCAPTCHA->setSize('compact');

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

reCAPTCHA v2 Example

<?php
require 'vendor/autoload.php';
use Holduix\Component\reCAPTCHA;

$reCAPTCHA = new reCAPTCHA('your-site-key', 'your-secret-key', 'v2');
$reCAPTCHA->setTheme('dark');
$reCAPTCHA->setLanguage('fr');
?>

<html>
<head>
    <title>reCAPTCHA v2 Example</title>
    <?php echo $reCAPTCHA->getScript(); ?>
</head>
<body>

<?php
if (isset($_POST['name'])) {
    if ($reCAPTCHA->isValid($_POST['g-recaptcha-response'])) {
        echo '<p>Captcha valide !</p>';
    } else {
        echo '<p>Erreur de captcha :</p>';
        var_dump($reCAPTCHA->getErrorCodes());
    }
}
?>

<form action="#" method="POST">
    <input type="text" name="name" placeholder="Nom">
    <?php echo $reCAPTCHA->getHtml(); ?>
    <input type="submit" value="Envoyer">
</form>

</body>
</html>

reCAPTCHA v3 Example

<?php
require 'vendor/autoload.php';
use Holduix\Component\reCAPTCHA;

$reCAPTCHA = new reCAPTCHA('your-site-key', 'your-secret-key', 'v3');
$reCAPTCHA->setScoreThreshold(0.7); // Seuil personnalisé
?>

<html>
<head>
    <title>reCAPTCHA v3 Example</title>
    <?php echo $reCAPTCHA->getScript(); ?>
</head>
<body>

<?php
if (isset($_POST['name'])) {
    if ($reCAPTCHA->isValid($_POST['g-recaptcha-response'])) {
        echo '<p>Captcha valide !</p>';
    } else {
        echo '<p>Erreur de captcha :</p>';
        var_dump($reCAPTCHA->getErrorCodes());
    }
}
?>

<form action="#" method="POST">
    <input type="text" name="name" placeholder="Nom">
    <?php echo $reCAPTCHA->getHtml(); ?>
    <input type="submit" value="Envoyer">
</form>

</body>
</html>