Welcome to the Math Captcha Controller! Enhance your form security with our simple yet effective math-based CAPTCHA system. Designed to blend seamlessly into your Laravel projects, this controller ensures your forms are protected from spam while providing a user-friendly experience.
- Dynamic CAPTCHA Generation: Generate random math problems with customizable difficulty.
- Easy Integration: Effortlessly add the CAPTCHA to your Blade templates.
- Session Management: Securely store CAPTCHA data in the session.
- Customizable Input and Script: Personalize the CAPTCHA input field and script behavior.
- Validation Logic: Ensure user input matches the CAPTCHA solution with robust
#Quick Start
Add this route to refresh the captcha:
// In App\Http\Controllers\MathCaptchaController
Route::get('get-captcha', [MathCaptchaController::class, 'resetCaptcha'])->name('get.captcha');To show the captcha input in a Blade template:
{!! \App\Http\Controllers\MathCaptchaController::input() !!}You can customize the captcha input by passing parameters:
{!! \App\Http\Controllers\MathCaptchaController::input('captcha_field', 'target_element', 'custom_classes') !!}captcha_field: The name of the captcha input field (default is "captcha").target_element: The parent element for the captcha input (optional).custom_classes: Any additional classes for the captcha input field (optional).
To include the captcha script in a Blade template:
{!! \App\Http\Controllers\MathCaptchaController::getScript() !!}You can customize the captcha script by specifying the selector:
{!! \App\Http\Controllers\MathCaptchaController::getScript('#custom_selector') !!}#custom_selector: The jQuery selector where the new HTML code with input will be loaded (default is#captcha-wrapper).
To validate the captcha in your controller:
$cvalidate = MathCaptchaController::validate($request);
if ($cvalidate !== true) {
// Your logic here if captcha is invalid
}Customize the number range and operators directly in the controller:
// Modify these variables in the MathCaptchaController
public static $min = 1; // Minimum number
public static $max = 20; // Maximum number
public static $operators = ['+', '-']; // Operators listSet custom error messages for captcha validation:
$cvalidate = MathCaptchaController::validate($request, 'captcha', 'Incorrect answer, try again.');
if ($cvalidate !== true) {
// Your logic here if captcha is invalid
}