A locally produced Captcha.
- Generates captcha image.
- Configurable code length.
- Configurable time sensitivity.
- Simple API for validating token and codes.
- PHP 8.5+
- PHP Imagick
$ composer require netherphp/captcha
Get a new fully rendered captcha using the ::Fresh() factory method. Get a new captcha where you can set settings before rendering with the ::New() factory method.
<?php
// prepare new captcha that is fully rendered.
$Cap = Nether\Captcha::Fresh();
// prepare captcha that can be configured before rendering.
$Cap = Nether\Captcha::New();
$Cap->SetTimeToExpire(60);
$Cap->Generate();
?>Render the image in the form you want to protect with the captcha.
<div>
<div>
<img src="<?php echo $Cap->GetImageEncoded() ?>" title="Captcha Image" />
</div>
<div>
<p>Enter code from above image.</p>
<input type="text" name="<?php echo $Cap->GetCodeFieldName() ?>" />
<input type="hidden" name="<?php echo $Cap->GetTokenFieldName() ?>" value="<?php echo $Cap->GetTokenFieldValue() ?>" />
</div>
</div>Use the ::ValidateFromInput() method to automatically find the captcha fields
from a dump of input with considerations handling the raw input automatically.
$CaptchaOK = Nether\Captcha::ValidateFromInput($_POST);
if(!$CaptchaOK) {
echo 'failed to validate captcha.';
return;
}Use the ::Validate() method to manually validate known token and codes.
$CaptchaOK = Nether\Captcha::Validate(
$_POST['nether-captcha-token'],
$_POST['nether-captcha-code']
);
if(!$CaptchaOK) {
echo 'failed to validate captcha.';
return;
}- Include front end templating.