mulertech/captcha 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mulertech/captcha

最新稳定版本:v1.0.0

Composer 安装命令:

composer require mulertech/captcha

包简介

Symfony bundle providing a self-hosted image-based math captcha for bot protection

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub PHPStan Action Status GitHub Security Action Status Total Downloads Test Coverage

A self-hosted, image-based math captcha Symfony bundle. No external service required. Generates a random arithmetic operation rendered as a low-quality JPEG with noise, validated server-side via session. Protects contact and quote forms against bots without any third-party dependency.

Requirements

  • PHP 8.4+
  • ext-gd
  • Symfony 6.4, 7.x or 8.x

Installation

composer require mulertech/captcha

The form theme is automatically registered — no additional configuration required.

Routes

Import the bundle routes in your application:

# config/routes/mulertech_captcha.yaml
mulertech_captcha:
    resource: "@MulerTechCaptchaBundle/config/routes.yaml"

The bundle provides two endpoints:

Route Path Description
mulertech_captcha_image GET /captcha/image?token=xxx Returns the JPEG captcha image
mulertech_captcha_refresh GET /captcha/refresh Returns {token, imageUrl} JSON for JS refresh

Usage

1. Add CaptchaType to your form

use MulerTech\CaptchaBundle\Form\CaptchaType;

$builder->add('captcha', CaptchaType::class);

The field is automatically mapped: false and includes the ValidCaptcha constraint.

2. Render in your Twig template

Render the field before the submit button. The widget displays the captcha image, a refresh button (JavaScript, no page reload), and the answer input:

{{ form_row(form.captcha) }}
<button type="submit">Envoyer</button>
{{ form_end(form) }}

3. Process the form in your controller

No special handling required. $form->isValid() returns false if the captcha answer is wrong or expired, with a localized error message on the captcha field.

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    // captcha passed — process the form
}

Error messages

Situation Message
Wrong answer Le code de vérification est incorrect.
Token expired / missing Le code de vérification a expiré, veuillez recommencer.

CSP nonce support

The bundle's inline <script> tag supports Content Security Policy nonces.

Automatic (with mulertech/csp-bundle)

If mulertech/csp-bundle is installed, the nonce is injected automatically — no additional configuration needed.

Manual

Pass the nonce explicitly via the csp_nonce option:

$builder->add('captcha', CaptchaType::class, [
    'csp_nonce' => $this->cspNonceGenerator->getNonce('main'),
]);

The nonce is added to the <script> tag rendered by the form theme:

<script nonce="abc123">...</script>

Security considerations

  • Token TTL: captcha tokens expire after 10 minutes.
  • Session limit: a maximum of 5 active tokens per session prevents session flooding.
  • Answer space: math operations produce answers in the range 1–30. Apply rate limiting at the application level (e.g., Symfony's RateLimiter) to prevent brute-force attempts.

Testing

./vendor/bin/mtdocker test-ai

统计信息

  • 总下载量: 101
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固