Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Captcha Plugin

Asyraf Wahi Anuar Updated 3 min read

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

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

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 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:

Terminal
composer require dereuromark/cakephp-captcha

Load Captcha Plugin:

Terminal
bin/cake plugin load Captcha

or manually load at …src/Application.php

Application.phpPHP
$this->addPlugin('Captcha');

Captcha Database Schema

Execute the following migration command:

Terminal
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 :)

Did this tutorial help?

258 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

CakePHP 4 Soft Delete

A soft delete marks a record as no longer active or valid without deleting it from the database. Soft deletion is a widely used pattern applied for business applications.

5 min read

CakePHP

CakePHP 4 Avoid Spam Using hCaptcha

Integrate hCaptcha with CakePHP 4 to protect unauthorized data submission from bots. Preventing spam in CakePHP 4 form.

2 min read

CakePHP

CakePHP 4 Audit Trail Using AuditStash Plugin

This plugin adds an "audit trail" to any of your Table classes in your application, allowing the web applications to record the creation, change, or deletion of any table's entities. In theory, it will store the old data before replacing it with the new data or content.

3 min read