BotDetect Laravel 5.1 CAPTCHA Integration Quickstart (BotDetect v3.0; deprecated)

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

Register GET and POST routes:
Route::get('example', 'ExampleController@getExample');
Route::post('example', 'ExampleController@postExample');
Create an instance of the Captcha class and generate a Captcha markup in your Controller:
<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
// Importing the BotDetectCaptcha class
use LaravelCaptcha\Integration\BotDetectCaptcha;

class ExampleController extends Controller 
{
  // get a captcha instance to handle for the example page
  private function getExampleCaptchaInstance() 
  {
    // Captcha parameters
    $captchaConfig = [
      'CaptchaId' => 'ExampleCaptcha', // a unique Id for the Captcha instance
      'UserInputId' => 'CaptchaCode', // Id of the Captcha code input textbox
      // The path of the Captcha config file is inside the config folder
      'CaptchaConfigFilePath' => 'captcha_config/ExampleCaptchaConfig.php'
    ];
    return BotDetectCaptcha::GetCaptchaInstance($captchaConfig);
  }

  public function getExample() 
  {
    // captcha instance of the example page
    $captcha = $this->getExampleCaptchaInstance();

    // passing Captcha Html to example view
    return view('example', ['captchaHtml' => $captcha->Html()]);
  }

}

Captcha configuration options (/config/captcha_config/ExampleCaptchaConfig.php):

<?php if (!class_exists('CaptchaConfiguration')) { return; }
  
// BotDetect PHP Captcha configuration options

$LBD_CaptchaConfig = CaptchaConfiguration::GetSettings();

$LBD_CaptchaConfig->CodeLength = 4;
$LBD_CaptchaConfig->ImageWidth = 250;
$LBD_CaptchaConfig->ImageHeight = 50;

Showing a Captcha challenge in a View:

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

  {!! $captchaHtml !!}
  <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;
// Importing the BotDetectCaptcha class
use LaravelCaptcha\Integration\BotDetectCaptcha;

class ExampleController extends Controller 
{
  // get a captcha instance to handle for the example page
  private function getExampleCaptchaInstance() {
    // Captcha parameters
    $captchaConfig = [
      'CaptchaId' => 'ExampleCaptcha', // a unique Id for the Captcha instance
      'UserInputId' => 'CaptchaCode', // Id of the Captcha code input textbox
      // The path of the Captcha config file is inside the config folder
      'CaptchaConfigFilePath' => 'captcha_config/ExampleCaptchaConfig.php'
    ];
    return BotDetectCaptcha::GetCaptchaInstance($captchaConfig);
  }

  public function getExample() 
  {
    // captcha instance of the example page
    $captcha = $this->getExampleCaptchaInstance();

    // passing Captcha Html to example view
    return view('example', ['captchaHtml' => $captcha->Html()]);
  }

  public function postExample(Request $request) 
  {
    // captcha instance of the example page
    $captcha = $this->getExampleCaptchaInstance();

    // 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.1 Captcha integration how to guide.

Other BotDetect PHP CAPTCHA Quickstarts

PHP CAPTCHA Laravel 5.0 CAPTCHA CodeIgniter 3.0 CAPTCHA
CakePHP 3.0 CAPTCHA Laravel 4.2 CAPTCHA CodeIgniter 2.2 CAPTCHA
CakePHP 2.6 CAPTCHA    


Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v3.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v4) is BotDetect v4 Captcha documentation index.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v4.0 page.