captcha-com/captcha
Laravel/PHP client for captcha.com. Helps you integrate CAPTCHA solving/verification into your app with simple API calls, configuration support, and helper methods for common workflows like submitting challenges and retrieving results.
Installation
composer require captcha-com/captcha
Add the service provider and facade to config/app.php:
'providers' => [
// ...
Captcha\BotDetect\BotDetectProvider::class,
],
'aliases' => [
// ...
'Captcha' => Captcha\BotDetect\Facades\Captcha::class,
],
Basic Usage Generate a CAPTCHA in a controller:
use Captcha\Facades\Captcha;
public function showForm()
{
$captcha = Captcha::create();
return view('form', ['captcha' => $captcha]);
}
Render the CAPTCHA in a Blade view:
{!! $captcha->getHtml() !!}
Validation Validate user input in a form request or controller:
$userInput = $request->input('captcha_code');
if (!Captcha::validate($userInput)) {
return back()->withErrors(['captcha' => 'Invalid CAPTCHA']);
}
Generate and Display
// In controller
$captcha = Captcha::create(['captchaType' => 'SimpleCaptcha']);
return view('contact', ['captcha' => $captcha]);
<!-- Blade -->
<div class="captcha-container">
{!! $captcha->getHtml() !!}
</div>
Validate Submission
public function submitForm(Request $request)
{
$validated = $request->validate([
'captcha_code' => 'required|captcha',
]);
// Proceed if valid
}
(Note: The captcha rule is auto-registered by the package.)
Styling
Override default CSS via config (config/captcha.php):
'captcha' => [
'captchaType' => 'SimpleCaptcha',
'customStyles' => [
'backgroundColor' => '#f5f5f5',
'fontFamily' => 'Arial',
],
],
Advanced CAPTCHA Types
Use MathCaptcha or PuzzleCaptcha for stronger protection:
$mathCaptcha = Captcha::create(['captchaType' => 'MathCaptcha']);
Integration with Forms Use Laravel’s form helpers for consistency:
<form method="POST" action="/submit">
@csrf
<div class="captcha-container">
{!! $captcha->getHtml() !!}
</div>
<input type="text" name="captcha_code" required>
<button type="submit">Submit</button>
</form>
Headless CAPTCHA Generation Generate a CAPTCHA for APIs without rendering HTML:
$captcha = Captcha::create(['returnAs' => 'code']);
$code = $captcha->getCode(); // Store in session/DB
Dynamic CAPTCHA Refresh Refresh CAPTCHA on failed attempts:
public function refreshCaptcha()
{
$captcha = Captcha::create();
return response()->json(['html' => $captcha->getHtml()]);
}
// Frontend (e.g., AJAX)
fetch('/refresh-captcha').then(response => {
document.querySelector('.captcha-container').innerHTML = response.html;
});
Session Expiry
config/captcha.php:
'sessionExpiryMinutes' => 60,
Captcha::clearExpiredCodes();
Case Sensitivity
'caseSensitive' => true,
CSRF Conflicts
@csrf is included in forms. The package does not handle CSRF protection.Image Paths
'imagePath' => storage_path('app/captcha'),
php artisan storage:link if storing in public.Validation Failures
$captcha = Captcha::create();
logger("Expected CAPTCHA: " . $captcha->getCode());
captcha_code is default).Image Not Displaying
imagePath is writable:
chmod -R 755 storage/app/captcha
open_basedir restrictions if using shared hosting.Performance
$captchaHtml = Cache::remember('captcha_html', 30, function () {
return Captcha::create()->getHtml();
});
Custom CAPTCHA Logic
Extend the base Captcha class:
namespace App\Captcha;
use Captcha\BotDetect\Captcha as BaseCaptcha;
class CustomCaptcha extends BaseCaptcha
{
public function __construct()
{
parent::__construct(['customOption' => true]);
}
}
Register in config/captcha.php:
'captchaClass' => App\Captcha\CustomCaptcha::class,
Event Hooks Listen for CAPTCHA generation/validation:
// In a service provider
Captcha::onGenerate(function ($captcha) {
logger("CAPTCHA generated: " . $captcha->getCode());
});
Multi-Language Support Localize CAPTCHA text (e.g., math problems):
$captcha = Captcha::create([
'language' => 'es', // Spanish
'mathOperators' => ['+', '-', '×'],
]);
throttle middleware to limit CAPTCHA refreshes.aria-label to CAPTCHA containers for screen readers:
<div class="captcha-container" aria-label="CAPTCHA verification">
{!! $captcha->getHtml() !!}
</div>
Captcha::fake() in PHPUnit to mock CAPTCHAs:
public function test_captcha_validation()
{
Captcha::fake('test123');
$this->post('/submit', ['captcha_code' => 'test123'])->assertRedirect('/success');
}
How can I help you explore Laravel packages today?