Difference between #2 and #3 of
Show captcha after <N> unsuccessfull attempts

Revision #3 has been created by zitter on Jun 13, 2012, 10:45:11 AM with the memo:

minor formatting changes + references
« previous (#2)

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>
13 4
17 followers
Viewed: 24 127 times
Version: 1.1
Category: How-tos
Written by: zitter
Last updated by: zitter
Created on: Jun 12, 2012
Last updated: 5 years ago
Update Article

Revisions

View all history