BotDetect Laravel 5.0 CAPTCHA Integration Quickstart

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"
]

Step 3: Publish Captcha configuration file:

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

The captcha.php file is already put in Laravel's config folder now.

2. Show a Captcha Challenge on the 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.0 Captcha integration how to guide.

Other BotDetect PHP CAPTCHA Quickstarts

PHP CAPTCHA Laravel 5.2 CAPTCHA Symfony 3 CAPTCHA
CakePHP 3 CAPTCHA Laravel 5.1 CAPTCHA Symfony2 CAPTCHA
CakePHP 2 CAPTCHA Laravel 4.2 CAPTCHA  
CodeIgniter 2.2 CAPTCHA    

BotDetect Laravel CAPTCHA System Requirements

Laravel

Supported Laravel Versions:

  • Laravel 5.0.0+