Installation
composer require arcaptcha/arcaptcha-php
Add the service provider to config/app.php (if not auto-discovered):
'providers' => [
Arcaptcha\ArcaptchaServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Arcaptcha\ArcaptchaServiceProvider"
Update config/arcaptcha.php with your ArCaptcha API keys:
'api_key' => env('ARCAPTCHA_API_KEY'),
'site_key' => env('ARCAPTCHA_SITE_KEY'),
First Use Case: Verify a CAPTCHA
use Arcaptcha\Arcaptcha;
$response = Arcaptcha::verify($request->input('arcaptcha_token'));
if ($response->success()) {
// CAPTCHA is valid, proceed with form submission
}
Displaying the CAPTCHA Add this to your Blade template:
{!! ArCaptcha::render() !!}
Or use the JS snippet from ArCaptcha’s dashboard.
Form Submission Validation Use Laravel’s validation rules with a custom rule:
use Arcaptcha\ArcaptchaValidationRule;
$request->validate([
'arcaptcha_token' => new ArcaptchaValidationRule(),
]);
Dynamic CAPTCHA Rendering Conditionally render CAPTCHA based on user roles or actions:
@if(auth()->user()->is_vip)
<!-- Skip CAPTCHA for VIPs -->
@else
{!! ArCaptcha::render() !!}
@endif
API Integration For API endpoints, verify CAPTCHA tokens in middleware:
namespace App\Http\Middleware;
use Arcaptcha\Arcaptcha;
use Closure;
class VerifyArcaptcha
{
public function handle($request, Closure $next)
{
$response = Arcaptcha::verify($request->arcaptcha_token);
if (!$response->success()) {
return response()->json(['error' => 'Invalid CAPTCHA'], 403);
}
return $next($request);
}
}
Rate Limiting CAPTCHA Attempts
Combine with Laravel’s throttle middleware to limit CAPTCHA verification attempts:
Route::post('/submit', [Controller::class, 'submit'])
->middleware(['throttle:5,1', 'verified.arcaptcha']);
Laravel Mix/Webpack If using ArCaptcha’s JS widget, ensure it’s loaded after jQuery:
// resources/js/app.js
window.$ = window.jQuery = require('jquery');
require('arcaptcha-widget');
Caching Responses Cache CAPTCHA verification responses for a short duration (e.g., 5 minutes) to reduce API calls:
$response = Cache::remember("arcaptcha_{$token}", 300, function() use ($token) {
return Arcaptcha::verify($token);
});
Multi-Language Support
Use ArCaptcha’s lang parameter to support multiple languages:
{!! ArCaptcha::render(['lang' => app()->getLocale()]) !!}
API Key Leaks
ARCAPTCHA_API_KEY or ARCAPTCHA_SITE_KEY in client-side code or logs.Token Expiry
IP-Based Rate Limits
Missing CSRF Protection
VerifyCsrfToken middleware to prevent CSRF attacks.False Positives/Negatives
Enable Debug Mode
Set debug to true in config/arcaptcha.php to log API responses:
'debug' => env('APP_DEBUG', false),
Check API Response Codes ArCaptcha returns HTTP codes:
200: Success.400: Invalid token.429: Rate limit exceeded.500: Server error.
Log $response->getStatusCode() for troubleshooting.Test with Known Tokens
Use ArCaptcha’s test tokens (e.g., 03AHJ6_1234567890ABCDEF0123456789ABCDEF) to verify your setup:
$response = Arcaptcha::verify('03AHJ6_1234567890ABCDEF0123456789ABCDEF');
Custom Validation Messages
Override default validation messages in app/Providers/AppServiceProvider.php:
use Arcaptcha\ArcaptchaValidationRule;
ArcaptchaValidationRule::extend(function ($message, $attribute, $rule, $parameters) {
return 'custom CAPTCHA error message';
});
Extend the Response Object
Add custom methods to the ArcaptchaResponse class by extending it:
namespace App\Services;
use Arcaptcha\ArcaptchaResponse;
class CustomArcaptchaResponse extends ArcaptchaResponse
{
public function isHuman()
{
return $this->success() && $this->getScore() > 0.9;
}
}
Bind it in AppServiceProvider:
Arcaptcha::setResponseClass(CustomArcaptchaResponse::class);
Webhook Integration Use ArCaptcha’s webhook API to log verification events. Set up a Laravel route to handle webhook payloads:
Route::post('/arcaptcha-webhook', [WebhookController::class, 'handle']);
Proxy Support If behind a proxy, configure ArCaptcha’s HTTP client to respect it:
Arcaptcha::setClient(new \GuzzleHttp\Client([
'proxy' => 'http://your-proxy:port',
]));
HTTPS Requirement
ArCaptcha’s API requires HTTPS. Ensure your Laravel app uses HTTPS in production (e.g., via APP_URL in .env):
APP_URL=https://yourdomain.com
CORS Issues
If using ArCaptcha’s JS widget, ensure your Laravel app’s CORS settings allow requests to ArCaptcha’s domain. Use the fruitcake/laravel-cors package if needed.
Locale Fallback
ArCaptcha defaults to en if the specified language isn’t supported. Test with unsupported locales to avoid unexpected behavior.
How can I help you explore Laravel packages today?