Search by

sugiphp / captcha

tzappa

SugiPHP CAPTCHA Component

Package info

github.com/SugiPHP/Captcha

pkg:composer/sugiphp/captcha

Statistics

Installs: 252

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

3.1.0 2026-11-18 12:04 UTC

This package is auto-updated.

Last update: 2026-11-18 12:25:16 UTC


README

Image CAPTCHA in two flavours, sharing the same interface (CaptchaInterface):

  • Crypt - does not need sessions
  • Session - keeps the code in the session; no secret key or database needed

Crypt

The code from the image travels with the form in an encrypted and signed form (the checksum), together with the time it was generated at. On submit the user input is checked against the checksum. Each CAPTCHA is accepted only once - the used ones are remembered (in a database or in a cache) until they expire, which by default happens one hour after they are generated.

Installation

composer require sugiphp/captcha

Requires PHP 7.4+ with the GD (with FreeType support), mbstring and openssl extensions.

Usage

Without sessions

use SugiPHP\Captcha\Crypt;
use SugiPHP\Captcha\PdoUsedKeys;

// $pdo is a PDO connection to a database having the table from schema.sql
$captcha = new Crypt(new PdoUsedKeys($pdo), $secret);

// showing the form
$captcha->create();
$image = "data:image/jpeg;base64," . base64_encode($captcha->getImage()); // <img src="...">
$checksum = $captcha->getChecksum();                                      // <input type="hidden" ...>

// processing the form
if (!$captcha->verify($_POST["captcha"], $_POST["checksum"])) {
    echo $captcha->getLastErrorMessage();
}

$secret is a private string, at least 32 characters long. A full working form is in examples/index.php.

With sessions

use SugiPHP\Captcha\Session;

session_start();
$captcha = new Session();

// showing and processing the form is the same as above

Here the checksum is just a random token pointing to the code in the session, so several forms (browser tabs) can be opened at the same time. Each CAPTCHA can be checked only once, no matter if the answer is right or wrong. A full working form is in examples/session.php.

If the application does not use $_SESSION directly, pass the array to work with as a second parameter: new Session($config, $sessionData).

Used codes storage (Crypt)

  • PdoUsedKeys - any PDO connection. Create the table from schema.sql.
  • CacheUsedKeys - any PSR-6 cache pool (composer require psr/cache and an implementation).
  • SimpleCacheUsedKeys - any PSR-16 cache (composer require psr/simple-cache and an implementation).
  • or your own UsedKeysInterface implementation.

Configuration

The configuration is an array (third constructor parameter of Crypt, first of Session) with any of:

Option Default Description
width 120 width of the image in pixels
height 40 height of the image in pixels
length 5 number of chars in the code
charset 0123456789 chars used for the code (UTF-8)
font bundled TTF font absolute path to a TTF font
quality 15 JPEG quality
lifetime 3600 seconds the code is valid for
cipher aes-256-cbc Crypt only: openssl cipher used to encrypt the code
session_key sugiphp_captcha Session only: where in the session the codes are kept
max_pending 10 Session only: how many not checked codes are kept in the session

Upgrading from 1.x

The SugiPHP\Captcha\Captcha class from 1.x is removed in 3.0. Version 2 is the bridge between 1.x and 3.x: it has both the old class and the new API, so the upgrade can be done in steps.

  1. Update to ^2.0 (PHP 7.4+ is required). Existing code continues to work, but creating a Captcha instance triggers an E_USER_DEPRECATED error.

  2. Replace the usages of Captcha with Session (the closest to what 1.x does - the code stays in the session) or with Crypt:

    1.x (Captcha) 2.x / 3.x
    new Captcha($config) new Session($config) or new Crypt($provider, $secret, $config)
    build() create()
    get() getImage()
    base64() "data:image/jpeg;base64," . base64_encode(getImage())
    output() header("Content-Type: image/jpeg"); echo getImage();
    $_SESSION["captcha"] = getCode() hidden form field with getChecksum()
    $_SESSION["captcha"] === $input verify($input, $checksum)
    setCode(), the code option not available - the code is always random

    Note that the default charset is now digits only. Pass "charset" => "abcdefhjkmnprstuvwxyz23456789" to keep the old one.

  3. When there are no more deprecation messages update to ^3.0, where the Captcha class is removed.