承接 holduix/recaptcha 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

holduix/recaptcha

最新稳定版本:2.0.0

Composer 安装命令:

composer require holduix/recaptcha

包简介

Google reCAPTCHA integration for PHP (supports v2 and v3)

README 文档

README

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>

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-only
  • 更新时间: 2025-01-12