Search by

netherphp / captcha

bobmagicii

Provides a basic captcha system.

Package info

github.com/netherphp/captcha

pkg:composer/netherphp/captcha

Statistics

Installs: 23

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v0.0.6 2026-11-17 04:29 UTC

This package is auto-updated.

Last update: 2026-11-17 04:34:19 UTC


README

Packagist Build Status codecov

A locally produced Captcha.

Features

  • Generates captcha image.
  • Configurable code length.
  • Configurable time sensitivity.
  • Simple API for validating token and codes.

Requirements

  • PHP 8.5+
  • PHP Imagick

Install

$ composer require netherphp/captcha

Usage

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;
}

Todo

  • Include front end templating.