Changes
Title
unchanged
Show captcha after <N> unsuccessfull attempts
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
session, Authentication, security, form
Content
changed
[...]
Basically, you need three things:
- in the model, you have to add captcha field as a required field in the rules() method
- in the controller, you have to create a different LoginForm model if number of unsuccessfull attempts are greater than <N>**N**
- in the view, you have to show captcha field if number of unsuccessfull attempts are greater than
<N>**N**
In the LoginForm model, you can use 'scenario' to set different required fields, so:[...]
```php
private function captchaRequired()
{
return Yii::app()->session->itemAt('captchaRequired') >= $this->attempts;
}[...]
Note that:
- if function *captchaRequired()* returns true create LoginForm with scenario 'captchaRequired', else create LoginForm with default scenario. This is useful because in protected/models/LoginForm.php we have set two different required fields depending on scenario:[...]
```php
<?php if($model->scenario == 'captchaRequired'): ?>
```
Easy, uh? ;)// code to show captcha
<?php endif; ?>
```
Easy, uh? ;)
References
----------
<http://www.yiiframework.com/forum/index.php/topic/21561-captcha-custom-validation>
<http://drupal.org/node/536274>