BotDetect Laravel 5.6 CAPTCHA Integration Quickstart

Unlike Recaptcha the Stalker -- BotDetect CAPTCHA works in China! Licensable source-code; self-hosted -- doesn't stalk -- nor does it slurp your form-data! Think: GDPR & LGPD!

1. Installing the BotDetect Laravel Captcha Composer Package

Path Aliases Used Throughout This Guide

  • <MY_LARAVEL_WEBROOT>: the root of the application install (same as the base_path() Laravel helper)
  • <MY_LARAVEL_APP>: the app directory (same as the app_path() Laravel helper)
  • <BD_LARAVEL_PACK>: the downloaded and extracted contents of the BotDetect Captcha Laravel Package

Note: If you do not have Composer yet, you can install it by following the instructions on https://getcomposer.org

If you have Git installed on your development machine, you should use the following installation procedure:

Step 1: Install the BotDetect Laravel Captcha Composer Package

Run the following command in your application's root directory:

Step 2: Register the Laravel Captcha service provider (<MY_LARAVEL_WEBROOT>/config/app.php):

"providers" => [
  ...
  LaravelCaptcha\Providers\LaravelCaptchaServiceProvider::class
]

2. Show a Captcha Challenge on the Form

a) If Laravel is a backend for your js-based frontend, perhaps you should start with a guide that describes integration from js-framework point of view: jQuery, Angular, AngularJS, or React.

b) If you are using Laravel forms, here is how to configure Captcha options, and display Captcha in your Laravel form:

Register GET and POST routes:
Route::get('example', 'ExampleController@getExample');
Route::post('example', 'ExampleController@postExample');

Captcha configuration options (/config/captcha.php):

<?php if (!class_exists('CaptchaConfiguration')) { return; }

// BotDetect PHP Captcha configuration options

return [
  // Captcha configuration for example page
  'ExampleCaptcha' => [
    'UserInputID' => 'CaptchaCode',
    'ImageWidth' => 250,
    'ImageHeight' => 50,
  ],

];

Showing a Captcha challenge in a View:

<link href="{{ captcha_layout_stylesheet_url() }}" type="text/css" rel="stylesheet">
</head>
  
  [...]

  {!! captcha_image_html('ExampleCaptcha') !!}
  <input type="text" id="CaptchaCode" name="CaptchaCode">

3. Check User Input During Form Submission

When a form is submitted, Captcha validation result must be checked in a Controller code:
<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller
{
  public function getExample()
  {
    return view('example');
  }

  public function postExample(Request $request)
  {
    // validate the user-entered Captcha code when the form is submitted
    $code = $request->input('CaptchaCode');
    $isHuman = captcha_validate($code);

    if ($isHuman) {
      // TODO: Captcha validation passed, perform protected  action
    } else {
      // TODO: Captcha validation failed, show error message
    }
  }
 
}

In-Depth Laravel CAPTCHA Instructions and Explanations

Detailed Laravel Captcha instructions and explanations can be found in the Laravel 5.6 Captcha integration how to guide.

Other BotDetect PHP CAPTCHA Quickstarts

PHP CAPTCHA CodeIgniter 3 CAPTCHA Symfony 3 CAPTCHA
CakePHP 3 CAPTCHA CakePHP 2 CAPTCHA Symfony2 CAPTCHA

BotDetect Laravel CAPTCHA System Requirements

Laravel

Supported Laravel Versions:

  • Laravel 5.6.0+