# CakePHP 4 Captcha Plugin

> Applying CakePHP Captcha Plugin to web application project. Captcha is a simple solution to block unwanted bot responses to your public available form.

- URL: https://www.codethepixel.com/cakephp-4-captcha-plugin
- Author: Asyraf Wahi Anuar (https://www.codethepixel.com/author/asyraf-wahi-anuar)
- Published: 2021-09-14
- Updated: 2021-11-19
- Category: CakePHP
- Tested with: CakePHP 4.x
- Tags: CakePHP, Plugin, Captcha
- Reading time: 3 min

CAPTCHA is an acronym that stands for the Completely Automated Public Turing test to tell Computers and Humans Apart. CAPTCHAs are tools that can be used to distinguish between human users and artificial users such as bots. CAPTCHAs present tasks that are tough for machines to complete but relatively simple for people to complete. If you are building any public form in your web application, Captcha is a very useful tool to control the bot from submitting unnecessary responses. The [Captcha Plugin](https://github.com/dereuromark/cakephp-captcha) enable the developer to apply captcha in any form.

## Requirement

CakePHP 4+  
GD Library enable

## Download Captcha Plugin

Run any preferred console, then execute the following composer code:

```bash
$ composer require dereuromark/cakephp-captcha
```

Load Captcha Plugin:

```bash
$ bin/cake plugin load Captcha
```

or manually load at …src/Application.php

`Application.php`

```php
$this->addPlugin('Captcha');
```

## Captcha Database Schema

Execute the following migration command:

```bash
$ bin/cake migrations migrate -p Captcha
```

## Load Captcha Helper

```php
File location: …\src\View\AppView.php
    public function initialize(): void
    {
        $this->loadHelper('Paginator', ['templates' => 'paginator-templates']);
        $this->loadHelper('Captcha.Captcha');
    }
```

## Load Captcha Component

In your controller, add the following code:

```php
    public function initialize(): void
    {
        parent::initialize();
        $this->loadComponent('Paginator');
        $this->loadComponent('Captcha.Captcha', ['actions' => ['add']]);
        $this->viewBuilder()->setHelpers(['Captcha.Captcha' => ['ext' => 'png']]);
    }
```

## Captcha View

To render the captcha, add the following code into your form:

```php
<?php echo $this->Captcha->render(['placeholder' => __('Please solve the riddle')]); ?>
```

The form will render the captcha (simple math) and required user input before submitting the form response. If you are using the Authentication Plugin, you can use the following code in your controller to activate the Captcha only for an unauthenticated user.

```php
        if (!$this->getRequest()->getAttribute('identity')['id'] ?? null) {
            $this->loadComponent('Captcha.Captcha', ['actions' => ['add']]);
            $this->viewBuilder()->setHelpers(['Captcha.Captcha' => ['ext' => 'png']]);
        }
```

TIPS: Don’t forget to enable the GD library, or else, your captcha will not be rendered.

That’s all, happy coding :)
