BotDetect Laravel 4.2 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_APP>/config/app.php):

"providers" => array(
  ...
  "LaravelCaptcha\Providers\LaravelCaptchaServiceProvider"
)

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 (/app/config/captcha.php):

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

// BotDetect PHP Captcha configuration options

return array(
  // Captcha configuration for example page
  'ExampleCaptcha' => array(
    'UserInputID' => 'CaptchaCode',
    'ImageWidth' => 250,
    'ImageHeight' => 50,
  ),

);

Showing a Captcha challenge in a View:

  {{ HTML::style(captcha_layout_stylesheet_url()) }}
</head>

  [...]

   {{ captcha_image_html('ExampleCaptcha') }}
  {{ Form::text('CaptchaCode', null, array('id' => 'CaptchaCode')) }}

3. Check User Input During Form Submission

When a form is submitted, Captcha validation result must be checked in a Controller code:
<?php

class ExampleController extends BaseController {

  public function getExample() {
    return View::make('example');
  }

  public function postExample() {
    // validate the user-entered Captcha code when the form is submitted
    $code = Input::get('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 4.2 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 4.2.0+