The first problem that I had with Yii was the lack of a normal captcha. The default captcha did not suit me for several reasons:
- it was buggy (just pressing F5 sometimes it was displayed normally, sometimes an empty picture was displayed, sometimes with a single first symbol);
- the agorhythm itself seemed to me too simple (+ only one font is used);
- The code in the image was not updated when the page was reloaded.
Googling, I realized that I am not the only one to face such problems. And the version with recaptches is also “not an option” - it was impossible to fit it into the design I needed (or I just didn’t understand it well).
Because I usually use the KCAPTCHA captcha (http://captcha.ru/kcaptcha/) in my projects, then I decided to combine the existing captcha from Yii and KCAPTCHA. And that's what happened to me -
glavweb.ru/public/download/kcaptcha.zipUsing captcha
First of all, download the captcha to yourself from here
glavweb.ru/public/download/kcaptcha.zip .
Next, you need to unpack the archive in the folder “protected / extensions” of your Yii application. So what would you get structure:
protected/
extensions/
kcaptcha/
fonts/
KCaptchaAction.php
Because the extension contains only Action, and the captcha widget and the validator are standard, I will not describe the full captcha connection, this is quite well done in recipes -http: //www.yiiframework.ru/doc/cookbook/ru/form.captcha. The changes concern only the controller, add something like this to it:
public function actions()
{
return array(
'captcha'=>array(
'class' => 'application.extensions.kcaptcha.KCaptchaAction',
'maxLength' => 6,
'minLength' => 5,
'foreColor' => array(mt_rand(0, 100), mt_rand(0, 100),mt_rand(0, 100)),
'backColor' => array(mt_rand(200, 210), mt_rand(210, 220),mt_rand(220, 230))
)
);
}
')
Possible parameters captcha
alphabet - The
alphabet used. The default is "0123456789abcdefghijklmnopqrstuvwxyz". Do not change without changing font files.
allowedSymbols - Symbols used for drawing captcha, without similar characters (o => 0, 1 => l, i => j, t => f). The default is "23456789abcdeghkmnpqsuvxyz".
width - Captcha width. The default is 120. Do not change it unnecessarily, these parameters are optimal.
height - Captcha height. The default is 60. Do not change it unnecessarily, these parameters are optimal.
minLength - The minimum number of captcha characters. The default is 6.
maxLength - The maximum number of captcha characters. The default is 7.
fluctuationAmplitude - The vertical amplitude of a symbol's oscillation divided by 2. The default is 5.
noSpaces - Prevent spaces between characters. The default is true.
foreColor - Text color captcha in RGB format. Accepts a value either as an array or as a string separated by commas.
backColor - Background color for captcha in RGB format. Accepts a value either as an array or as a string separated by commas.
testLimit - The value of how many times the same captcha will be displayed. It only affects the wrong input captcha (a simple refresh is not affected). The default is 3.
fixedVerifyCode - Fixed verification code. This is mainly used in automated tests where we want to reproduce the same verification code each time we run tests. The default is null.
fontsDir - Absolute path to the font directory. The default is null, which means that the fonts supplied with the extension are used.
Updating the image code after reloading the page
But updating the image code after reloading the page turned out to be a bit more complicated. The hitch in the validator is “CCaptchaValidator”, or rather, in its method it is responsible for the client part. Even if we make an update of the verification code every time the captcha is loaded, then in js (generated by the validator), we still get the code from the previous picture. This is because the request for the picture comes after the page loads. Those. First, in js code, we get the image code stored in the sessions, then the request goes to <img>, the session is updated and as a result, the code from the image does not match the corresponding code in js (note, of course, that js is not the image code itself, but its hash ).
Exit here or rewrite the validator and update the checker in the validator when displayed in js ($ captcha-> getVerifyCode (true)) or delete the session with the code in action that is responsible for displaying the captcha form:
// Refresh captcha
Yii::import('application.extensions.kcaptcha.KCaptchaAction');
Yii::app()->session->remove(KCaptchaAction::SESSION_KEY);