BotDetect CakePHP 3 CAPTCHA Integration Quickstart

1. Installing the BotDetect CakePHP Captcha Composer Plugin

Path Aliases Used Throughout This Guide

  • <MY_CAKE_WEBROOT>: the root of the application install (same as the ROOT PHP CakePHP constant)
  • <BD_CAKE_PACK>: the downloaded and extracted contents of the BotDetect Captcha CakePHP 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 CakePHP Captcha Composer Plugin:

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

Step 2: Load the BotDetect CakePHP Captcha Composer Plugin in your application's bootstrap (<MY_CAKE_WEBROOT>/config/bootstrap.php):

Plugin::load('CakeCaptcha', ['routes' => true]);

2. Show a Captcha Challenge on the Form

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,
  ],
  
];
Load the BotDetect CakePHP component:
<?php namespace App\Controller;

use App\Controller\AppController;

class ExampleController extends AppController
{
  public function initialize()
  {
    parent::initialize();

    // load the Captcha component and set its parameter
    $this->loadComponent('CakeCaptcha.Captcha', [
      'captchaConfig' => 'ExampleCaptcha'
    ]);
  }
}

Showing a Captcha challenge in a View:

<!-- include the BotDetect layout stylesheet -->
<?= $this->Html->css(captcha_layout_stylesheet_url(), ['inline' => false]) ?>
</head>
  
  [...]

  <!-- show captcha image -->
  <?= captcha_image_html() ?>


  <!-- Captcha code user input textbox -->
  <?= $this->Form->input('CaptchaCode', [
    'label' => 'Retype the characters from the picture:',
    'maxlength' => '10',
    'style' => 'width: 270px;',
    'id' => 'CaptchaCode'
  ]) ?>

3. Check User Input During Form Submission

When a form is submitted, Captcha validation result must be checked in a Controller code:
if ($this->request->is('post')) {

  // validate the user-entered Captcha code
  $isHuman = captcha_validate($this->request->data['CaptchaCode']);

  // clear previous user input, since each Captcha code can only be validated once
  unset($this->request->data['CaptchaCode']);
 
  if ($isHuman) {
      // TODO: Captcha validation passed, perform protected action
  } else {
      // TODO: Captcha validation failed, show error message
  }
}

In-Depth CakePHP CAPTCHA Instructions and Explanations

Detailed CakePHP Captcha instructions and explanations can be found in the CakePHP 3 Captcha integration how to guide.

Other BotDetect PHP CAPTCHA Quickstarts

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

BotDetect CakePHP CAPTCHA System Requirements

CakePHP

Supported CakePHP Versions:

  • CakePHP 3.2.0+
  • CakePHP 3.1.0+
  • CakePHP 3.0.0+