BotDetect CakePHP 2 CAPTCHA Integration Quickstart

1. Copy CakePHP Captcha Plugin Files

Path Aliases Used Throughout This Guide

  • <MYCAKEAPP>: your CakePHP app directory (same as the APP_DIR PHP CakePHP constant)
  • <BDCAKEPACK>: the downloaded and extracted contents of the BotDetect Captcha CakePHP Package
  • Download the BotDetect Captcha CakePHP integration package and copy:
    <BDCAKEPACK>/bd-captcha-cakephp-2-examples/ app/Plugin/BotDetect
    to:
    <MYCAKEAPP>/Plugin/BotDetect
  • Copy BotDetect PHP Captcha library from:
    <BDCAKEPACK>/bd-captcha-cakephp-2-examples/ app/Lib
    to:
    <MYCAKEAPP>/Lib

2. Show a Captcha Challenge on the Form

Load the BotDetect Captcha CakePHP plugin in your application's bootstrap (<MYCAKEAPP>/Config/bootstrap.php):
CakePlugin::load(array(
  'BotDetect' => array('routes' => true)
));

Captcha configuration options (<MYCAKEAPP>/Config/captcha.php):

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

// BotDetect PHP Captcha configuration options

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

);
> Load the BotDetect CakePHP component:
class ExampleController extends AppController { // your controller
  
  public $components = array(
    'BotDetect.Captcha' => array(
      'captchaConfig' => 'ExampleCaptcha'
    )
  );
}
Show a Captcha challenge in a View:
echo $this->Html->css(captcha_layout_stylesheet_url(), array('inline' => false));
  
// display Captcha markup, wrapped in an extra div for layout purposes
echo $this->Html->div('captcha', captcha_image_html(), false);

// Captcha code user input textbox
echo $this->Form->input('CaptchaCode');
  

3. Check User Input During Form Submission

When 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 2 Captcha integration how to guide.

Other BotDetect PHP CAPTCHA Quickstarts

PHP CAPTCHA Laravel CAPTCHA Symfony CAPTCHA
CodeIgniter CAPTCHA

BotDetect CakePHP CAPTCHA System Requirements

CakePHP

Supported CakePHP Versions:

  • CakePHP 2.8.0+
  • CakePHP 2.7.0+
  • CakePHP 2.6.0+