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 challenge
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 CAPTCHA recipe. Alternatively you can take any form that uses CAPTCHA, as we are not modifying the existing code a lot.
We need to customize CCaptchaAction
, 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
protected/components/MathCaptchaAction.php
as follows:<?php class MathCaptchaAction...