gregwar/captcha
Generate CAPTCHA images in PHP with Gregwar CaptchaBuilder. Create, save, output, or embed captchas inline, retrieve and validate the phrase against user input, tweak distortion/background, and optionally build captchas resistant to OCR (with ocrad).
random_int()) directly addresses OWASP A03:2021 (Injection) and A07:2021 (Identification and Authentication Failures) by eliminating predictable CAPTCHA patterns. This is a must-have for:
$_SESSION or session() helper).Validator to check CAPTCHA input:
$validator = Validator::make($request->all(), [
'captcha' => ['required', function ($attribute, $value, $fail) {
$builder = new CaptchaBuilder();
if (!$builder->testPhrase($value)) {
$fail('CAPTCHA verification failed.');
}
}]
]);
@captcha directive for reusable markup:
Blade::directive('captcha', function () {
$builder = new CaptchaBuilder();
$_SESSION['captcha_phrase'] = $builder->getPhrase();
return "<?php echo {$builder->inline()}; ?>";
});
captcha:generate command for testing/pre-generation (e.g., for offline validation).$_SESSION (Laravel’s session() driver). For distributed systems, ensure session consistency (e.g., Redis).Log facade).random_int().ImageFileHandler was removed in v2.0.0; ensure no legacy code relies on it.phpinfo() if using custom PHP builds.buildAgainstOCR() require imagemagick and ocrad (shell_exec). Document these as optional dependencies.setMaxFrontLines(10)).PhraseBuilder alphabets).config('session.lifetime')).mt_rand()-based CAPTCHAs that need migration?microtime(true).)alt text for CAPTCHA images.)CaptchaBuilder in a service provider to centralize configuration:
public function register() {
$this->app->singleton('captcha', function () {
$builder = new CaptchaBuilder();
$builder->setMaxFrontLines(config('captcha.distortion.lines'));
return $builder;
});
}
use Gregwar\Captcha\CaptchaBuilder;
class CaptchaRule extends FormRequest {
public function passes($attribute, $value) {
$builder = app('captcha');
return $builder->testPhrase($value);
}
}
@captcha directive or inline output:
<img src="{{ captcha() }}" alt="CAPTCHA" class="captcha-img">
document.querySelector('.refresh-captcha').addEventListener('click', () => {
fetch('/refresh-captcha')
.then(response => response.text())
.then(html => {
document.querySelector('.captcha-img').src = html;
});
});
encrypt()):
$phrase = $builder->getPhrase();
$token = encrypt($phrase);
return response()->json(['token' => $token]);
CaptchaBuilder, gregwar/captcha, or manual GD-based implementations)./forgot-password, /admin/login).composer.json to gregwar/captcha:^2.1.0 and test in a staging environment.mt_rand()) with the new package in critical paths.deprecated() helper).config/captcha.php:
return [
'phrase_length' => 6,
'distortion' => [
'max_front_lines' => 5,
'max_behind_lines' => 3,
],
'image_type' => 'jpeg',
];
random_int().random_int() (e.g., ramsey/uuid).CaptchaBuilder in PHPUnit tests:
$mockBuilder = Mockery::mock(CaptchaBuilder::class);
$mockBuilder->shouldReceive('testPhrase')->andReturnTrue();
$this->app->instance(CaptchaBuilder::class, $mockBuilder);
How can I help you explore Laravel packages today?