trisnawan / captcha-gateway
Captcha Gateway is a PHP library that provides integration to secure your website forms with CloudFlare Turnstile or Google ReCaptcha.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/trisnawan/captcha-gateway
Requires
- php: >=7.4.0
This package is auto-updated.
Last update: 2025-12-02 18:18:24 UTC
README
Captcha Gateway is a PHP library that provides integration to secure your website forms with CloudFlare Turnstile or Google ReCaptcha.
To use CloudFlare Turnstile, please create a widget in CloudFlare Turnstile.
To use Google ReCaptcha, please create a Google Cloud Console project and then create ReCaptcha.
Instalation
composer require trisnawan/captcha-gateway
Initialization
use Trisnawan\CaptchaGateway\Captcha;
Hardcoded
$captcha = new Captcha("VENDOR", "PROJECT", "SITE_KEY", "SECRET_KEY");
Environment
captcha.vendor = VENDOR
captcha.widget = PROJECT
captcha.site.key = SITE_KEY
captcha.secret.key = SECRET_KEY
$captcha = new Captcha();
Note:
VENDOR,turnstileorrecaptchaPROJECT, ID Project Google ReCaptcha or Widget Name Cloudflare TurnstileSITE_KEY, Site Key or Public KeySECRET_KEY, Secret Key or API KEY
Form
// 1. load script echo $captcha->client->script(); // 2. render echo $captcha->client->render("ACTION", "INPUT_ID", "BUTTON_ID");
Note:
ACTION, action form, example: login, registration, chart, buy, etc.INPUT_ID, input id captcha-token (optional)BUTTON_ID, button submit id (optional)
Example:
<html>
<head>
<?= $captcha->client->script() ?>
</head>
<body>
<form method="post" action="login">
<input type="text" name="username">
<input type="text" name="password">
<?= $captcha->client->render("login-action", "login-captcha", "login-button") ?>
<button type="submit" id="login-button">Login</button>
</form>
</body>
</html>
Validation
// 1. VERIFY - default $validation = $captcha->server->verify("login-action"); // 1. VERIFY - manual token data $token = $_POST['captcha-token']; $validation = $captcha->server->verify("login-action", $token); // 1. VERIFY - manual token data + IP verification $ip = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? null; $token = $_POST['captcha-token']; $validation = $captcha->server->verify("login-action", $token, $ip); // 2. CHECK VALIDATION if(!$validation->isSuccess){ $messageError = $validation->errorMessage; $arrayErrors = $validation->errors; // show error }