I have two forms (login and register) on single page and they both required captcha. I know is not common but I cannot change this biz requirement because this is a special request. Currently they both shared the same captcha but I cannot refresh them independently. Anyone has any ideas?
Someone suggests that I would design the page so that only one displays and is used for both login and register functions. This is maybe an option but I want to make sure I can be done with two captchas.
i and softark seems give the solution same ! when i commit my post just see the softark has posted !
the key code is this :
/**
* Returns the session variable name used to store verification code.
* @return string the session variable name
*/
protected function getSessionKey()
{
return self::SESSION_VAR_PREFIX . Yii::app()->getId() . '.' . $this->getController()->getUniqueId() . '.' . $this->getId();
}
the validation use this key to check in the $session . “$this->getId()” is the captchaActionId , you just make it different !
I also have tried some, but in vain. I’m very sorry.
[EDIT]
OK, I think I got it.
Try this …
public function rules()
{
return array(
...
array('verifyCode1', 'captcha', ...
array('verifyCode2', 'verifycaptcha2', ...
);
}
public function verifycaptcha2($attribute, $params)
{
$captcha2 = Yii::app()->getController()->createAction('captcha2nd');
if (!$captcha2->validate($this->verifyCode2, false))
{
$this->addError('verifyCode2', 'invalid captcha.');
}
}
The validation for the 2nd captcha is performed by a dedicated function called ‘verifycaptcha2’ in which the proper instance of the 2nd captcha action is created by ‘Yii::app()->getController()->createAction(‘captcha2nd’)’.
One of the easiest ways to make captcha robust is, IMO, using non-ascii characters.
Although you have to limit the visitors to some people(e.g. Japanese only, Chinese only, … etc.), requiring non-ascii characters in captcha seems very effective.
Here’s an example that I’ve made with an extended yii captcha. It requires “Hirakana” of Japanese characters.