# Migrating from PHP (laravel-go-captcha)

> Each mews/captcha call and its Go equivalent, followed by every place where the Go port behaves differently.

- Package: laravel-go-captcha (Laravel for Go, alpha), go modules: `github.com/bowerbird-si/laravel-go-captcha`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: go.laravel.com v0.2.0 or later; Go: Go 1.27 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-go-captcha), [pkg.go.dev](https://pkg.go.dev/github.com/bowerbird-si/laravel-go-captcha@v0.1.0), [Changelog](https://oss.bowerbird.si/go-captcha/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-go-captcha/blob/7a10d1c56d2e1a3e3e6618d025657dda5d5385cf/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-go-captcha/blob/7a10d1c56d2e1a3e3e6618d025657dda5d5385cf/SECURITY.md)
- Based on: [mews/captcha](https://github.com/mewebstudio/captcha): The captcha package this ports: profiles, config keys, routes and validation, following version 3.5.1. Its bundled fonts ship with their own licences.
- PHP parity: core (52 covered, 35 diverged, 0 planned of 87 abilities; 44 fixtures; follows mews/captcha 3.5.1; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/go-captcha/docs/migrating-from-php/
- Source: https://github.com/bowerbird-si/laravel-go-captcha/blob/7a10d1c56d2e1a3e3e6618d025657dda5d5385cf/docs/migrating-from-php.md

Each mews/captcha call and its Go equivalent, followed by every place where the Go port behaves differently.

| PHP | Go |
| --- | --- |
| `'captcha' => 'required\|captcha'` | `rule.Field(&f.Captcha, rule.Required, captcha.Rule(r))` |
| `'captcha' => 'required\|captcha_api:'.$key.',math'` | `rule.Field(&f.Captcha, rule.Required, captcha.KeyedWith(key).Profile("math"))`, or `captcha.Keyed()` to read the key from the `captcha_key` field |
| `captcha_src('flat')` | `captcha.Src(ctx, "flat")`, or `{{captchaSrc "flat"}}` in a template |
| `captcha_img('flat', ['class' => 'x'])` | `captcha.Img(ctx, "flat", captcha.Attr{Name: "class", Value: "x"})`, or `{{captchaImg "flat" "class" "x"}}` |
| `captcha('flat')` | `captcha.Image(r, "flat")` |
| `captcha_check($value)` | `captcha.Check(r, value)` |
| `captcha_api_check($value, $key, 'math')` | `captcha.CheckKey(ctx, key, value, "math")` |
| `Captcha::create('default', true)` | `captcha.Issue(ctx, "default")`: `Key`, `Sensitive`, `DataURI()` |
| `config/captcha.php` | `captcha.New(func(cfg *captcha.Config) { … })`, or `captcha.ParsePHPConfig` |
| `CAPTCHA_DISABLE` | `CAPTCHA_DISABLE` |

## What behaves differently

These differences are deliberate. Each one is tested against the PHP behaviour.

**Security**

- **The API key is an opaque id.** With `encrypt` off, PHP's key was a bcrypt hash of the answer, so a client
  could check guesses offline. Nothing the client receives here can check a guess.
- **A challenge answers only its own profile.** PHP's session check accepted an image from any profile.
  Here a rule without a profile means `default`, so name the profile in the rule when the form shows another
  one.
- **The answer is never stored.** The store keeps a challenge record with a keyed verifier; a successful
  check marks the record used, and it's removed when its retention ends.
- **The session holds only a marker.** The challenge lives in the store, so an old cookie can't revive an
  earlier challenge. Requesting a keyed challenge never replaces a session one.
- **Wrong answers and image requests are limited** per client (see
  [Configuration](https://oss.bowerbird.si/go-captcha/docs/configuration/#limits)).
- **`captcha_img` escapes attribute values and accepts only safe attribute names.** PHP inserted them
  unescaped. PHP's helper also returned a string, so Blade's `{{ }}` escaped the whole tag; the Go helper
  returns markup a template doesn't escape again.

**Behaviour**

- **Missing answers fail.** PHP's rule passed a missing or empty captcha when `required` was left out.
- **`"0"` is a valid answer.** PHP's rule failed it.
- **Surrounding spaces are ignored.** Answers are trimmed before the comparison.
- **An unknown profile is a 404.** PHP served the class defaults for any name.
- **Settings never leak between profiles.** In PHP, a profile could inherit keys the previous profile set.
- **Invalid settings stop the application at startup**, instead of failing on each request.
- **`HEAD` issues nothing**, and images are sent with `Cache-Control: no-store`.
- **The JPEG `quality` setting is used.** PHP always encoded at 75.
- **Math profiles allow at most 3 attempts.** A sum has few possible answers.
- **The default message is "The :attribute is incorrect."** PHP showed the raw key `validation.captcha`.
- **Expiry:** a challenge without a deadline (`captcha.NoDeadline`) is still removed after
  `NoDeadlineLifetime` (24 hours by default). PHP's cache entry lived for `expire` seconds.

**Not ported**

- **The image driver setting.** Images are drawn by the package's own Go pipeline; `ParsePHPConfig` ignores
  `driver`.
- **Publishing a config file.** Laravel for Go has no config files: settings are set in code, and
  `ParsePHPConfig` reads an exported PHP config.
- **Lumen routes and output buffering.** They don't apply to Laravel for Go.

To move a config file, export it with `json_encode(config('captcha'))` and read it with
`captcha.ParsePHPConfig`.
