PHPackages                             tetthys/captcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. tetthys/captcha

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

tetthys/captcha
===============

Lightweight, framework-agnostic captcha generator and validator for PHP 8.3+.

0.0.1(8mo ago)035MITPHPPHP ^8.3

Since Oct 19Pushed 8mo agoCompare

[ Source](https://github.com/tetthys/captcha)[ Packagist](https://packagist.org/packages/tetthys/captcha)[ RSS](/packages/tetthys-captcha/feed)WikiDiscussions dev Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

tetthys/captcha
===============

[](#tetthyscaptcha)

> Lightweight, framework-agnostic captcha utility for PHP 8.3+.
> Provides a pluggable algorithm/validation interface and optional session storage for user verification.

---

📦 Installation
--------------

[](#-installation)

```
composer require tetthys/captcha
```

Requires **PHP 8.3+** (no helpers, no external dependencies).

---

🚀 Quick Example (Standalone PHP)
--------------------------------

[](#-quick-example-standalone-php)

```
use Tetthys\Captcha\Algorithms\SimpleMathCaptcha;
use Tetthys\Captcha\Services\CaptchaService;
use Tetthys\Captcha\Services\CaptchaSession;

$service = new CaptchaService(
    new SimpleMathCaptcha(),
    new CaptchaSession(),
);

// Generate a new captcha
$captcha = $service->new();
echo $captcha->question; // e.g. "3 + 5 = ?"

// Later...
$userInput = '8';
$isValid = $service->validateCurrent($userInput); // true or false
```

---

🧩 Core Concepts
---------------

[](#-core-concepts)

### `CaptchaAlgorithmInterface`

[](#captchaalgorithminterface)

Defines how a captcha is generated.

```
interface CaptchaAlgorithmInterface
{
    public function generate(): object; // { question, answer }
}
```

Example implementation:

```
(new SimpleMathCaptcha())->generate();
// => (object) [ 'question' => '7 + 2 = ?', 'answer' => '9' ]
```

---

### `CaptchaValidatorInterface`

[](#captchavalidatorinterface)

Defines how to check correctness.

```
interface CaptchaValidatorInterface
{
    public function validate(string $answer, string $input): bool;
}
```

---

### `CaptchaSession`

[](#captchasession)

Handles temporary storage of the correct answer. Uses Laravel’s `session()` helper if available, otherwise falls back to a simple array/object store.

```
$session = new CaptchaSession();
$session->set('42');
echo $session->pull(); // '42' (and then removed)
```

---

### `CaptchaService`

[](#captchaservice)

Coordinates generation, storage, and validation.

```
$service = new CaptchaService(
    new SimpleMathCaptcha(),
    new CaptchaSession(),
);

$q = $service->new();                // -> question only
$isValid = $service->validateCurrent('7'); // checks user input vs stored answer
```

---

⚙️ Integration with Laravel
---------------------------

[](#️-integration-with-laravel)

Add a simple service provider:

```
use Illuminate\Support\ServiceProvider;
use Tetthys\Captcha\Algorithms\SimpleMathCaptcha;
use Tetthys\Captcha\Services\CaptchaService;
use Tetthys\Captcha\Services\CaptchaSession;

final class CaptchaServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(CaptchaService::class, function () {
            return new CaptchaService(
                new SimpleMathCaptcha(),
                new CaptchaSession(),
            );
        });
    }
}
```

Register it in

- **Laravel 11/12:** `bootstrap/app.php`
- **Laravel 10:** `config/app.php` → `providers[]`

Usage in controller:

```
$captcha = app(CaptchaService::class)->new();        // -> { question }
$isValid = app(CaptchaService::class)->validateCurrent($request->input('captcha'));
```

---

🧪 Custom Algorithms
-------------------

[](#-custom-algorithms)

You can easily define your own captcha generator:

```
use Tetthys\Captcha\Contracts\CaptchaAlgorithmInterface;

final class WordCaptcha implements CaptchaAlgorithmInterface
{
    public function generate(): object
    {
        $word = substr(str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZ'), 0, 5);
        return (object) ['question' => $word, 'answer' => $word];
    }
}
```

Swap it in:

```
new CaptchaService(new WordCaptcha(), new CaptchaSession());
```

---

🧮 Example API Response
----------------------

[](#-example-api-response)

```
GET /api/captcha
→ { "question": "4 + 6 = ?" }

POST /api/captcha
body: { "input": "10" }
→ { "valid": true }

```

---

🪪 License
---------

[](#-license)

MIT © Tetthys Labs

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance60

Regular maintenance activity

Popularity7

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

257d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/115064626?v=4)[tetthys](/maintainers/tetthys)[@tetthys](https://github.com/tetthys)

### Embed Badge

![Health badge](/badges/tetthys-captcha/health.svg)

```
[![Health](https://phpackages.com/badges/tetthys-captcha/health.svg)](https://phpackages.com/packages/tetthys-captcha)
```

###  Alternatives

[marcosh/php-validation-dsl

A DSL for validating data in a functional fashion

483.9k](/packages/marcosh-php-validation-dsl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
