
Yii2 Application Development Cookbook
By :

A standard Yii Captcha is good enough to protect you from spam, but there are situations where you may want to customize it, such as the following:
You face a spam bot that can read image text and you need to add more security
You want to make it more interesting or easier to enter the Captcha text
In our example, we will modify Yii's Captcha so it will require the user to solve a really simple arithmetic puzzle instead of just repeating the text in an image.
As a starting point for this example, we will take the result of the Adding and customizing CaptchaWidget recipe. Alternatively, you can take any form that uses Captcha, as we are not modifying the existing code a lot.
We need to customize CaptchaAction
, which generates the code and renders its image representation. The code should be a random number and the representation should be an arithmetic expression that gives the same result:
Create an @app/components/MathCaptchaAction.php
action...
Change the font size
Change margin width
Change background colour