tourze / captcha-challenge-bundle
Symfony图片验证码验证模块
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/captcha-challenge-bundle
Requires
- php: ^8.1
- ext-gd: *
- gregwar/captcha: ^1.2
- nzo/url-encryptor-bundle: ^6.4
- psr/simple-cache: ^1.0|^2.0|^3.0
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/uid: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-lock-bundle: 0.1.*
- tourze/json-rpc-log-bundle: 0.1.*
- tourze/symfony-routing-auto-loader-bundle: 0.0.*
Requires (Dev)
- maglnet/composer-require-checker: ^4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:11:01 UTC
README
A Symfony Bundle that provides image captcha functionality for website security protection.
Features
- Generate random captcha codes and store challenge values in cache
- Provide image captcha generation and display
- Provide captcha verification and one-time consumption functionality
- Support JSON-RPC interface
Installation
Install via Composer:
composer require tourze/captcha-challenge-bundle
Configuration
Register the Bundle in your Symfony application:
// config/bundles.php return [ // ... Tourze\CaptchaChallengeBundle\CaptchaChallengeBundle::class => ['all' => true], ];
Ensure environment variables are configured:
# .env LOGIN_CHALLENGE_TYPE=captcha # Enable captcha functionality, set to 'null' to disable
Usage
Generating Captcha and Getting Image
// In your controller use Tourze\CaptchaChallengeBundle\Service\ChallengeService; class YourController extends AbstractController { public function generateCaptcha(ChallengeService $challengeService): Response { // Generate captcha $challengeKey = $challengeService->generateChallenge(); // Get captcha image URL $imageUrl = $challengeService->generateChallengeCaptchaImageUrl($challengeKey); return $this->json([ 'challengeKey' => $challengeKey, 'imageUrl' => $imageUrl, ]); } }
Verifying User Input Captcha
// In your controller use Tourze\CaptchaChallengeBundle\Service\ChallengeService; class YourController extends AbstractController { public function verifyCode( Request $request, ChallengeService $challengeService ): Response { $challengeKey = $request->request->get('challengeKey'); $userInput = $request->request->get('captchaCode'); // Verify and consume captcha $isValid = $challengeService->checkAndConsume($challengeKey, $userInput); if (!$isValid) { return $this->json(['success' => false, 'message' => 'Invalid captcha code']); } return $this->json(['success' => true]); } }
Advanced Usage
Custom Captcha Configuration
// Custom captcha generation with specific settings $challengeKey = $challengeService->generateChallenge(); // Generate image with custom parameters $imageUrl = $challengeService->generateChallengeCaptchaImageUrl($challengeKey);
Integration with Forms
// In your form type use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; $builder ->add('challengeKey', HiddenType::class) ->add('captchaCode', TextType::class, [ 'label' => 'Enter captcha code', 'required' => true, ]);
Technical Details
- Captcha uses 5-digit random numbers
- Captcha is stored in cache for 5 minutes
- After successful verification, it is immediately deleted from cache to prevent reuse
- Uses GD library to generate images, supports anti-OCR functionality
Requirements
- PHP 8.1+
- GD extension
- Symfony 6.4 framework
- PSR-16 cache implementation
- Gregwar/Captcha library
License
This Bundle is licensed under the MIT License. See the LICENSE file for details.