, , Soph. , , JavaScript. , , , .
- , , , . , , POST (GET request). , Soph , - , POST , .
Yii captcha. , system.web.widgets.captcha. CCaptcha CWidget, , - :
<?php
$this->widget("CCaptcha", array(
'buttonLabel' => "Generate another code",
'showRefreshButton' => false,
'clickableImage' => true
));
?>
CCaptchaAction, , , . , Yii -, Captcha CaptchaAction, CCaptcha CCaptchaAction components - .
.
run() CCaptchaAction , , if - , render_refreshed GET .
Captcha refresh - false, CCaptcha one. renderImage, , HTML- . , captcha, src img. refresh true render_refreshed.
CaptchaAction.php:
<?php
Yii::import("system.web.widgets.captcha.CCaptchaAction");
class CaptchaAction extends CCaptchaAction
{
const RENDER_REFRESHED_GET_VAR = "render_refreshed";
public function run()
{
if (isset($_GET[self::REFRESH_GET_VAR]))
{
$code = $this->getVerifyCode(true);
echo CJSON::encode(array(
'hash1' => $this->generateValidationHash($code),
'hash2' => $this->generateValidationHash(strtolower($code)),
'url'=> $this->getController()->createUrl($this->getId(), array(
'v' => uniqid()
)),
));
}
else if (isset($_GET[self::RENDER_REFRESHED_GET_VAR]))
{
$this->renderImage($this->getVerifyCode(true));
}
else
$this->renderImage($this->getVerifyCode());
Yii::app()->end();
}
}
Captcha.php:
<?php
Yii::import("web.system.widgets.CCaptcha");
class Captcha extends CCaptcha
{
public $refresh = false;
protected function renderImage()
{
if (!isset($this->imageOptions['id']))
$this->imageOptions['id'] = $this->getId();
if ($this->refresh)
{
$url = $this->getController()->createUrl($this->captchaAction, array(
'v' => uniqid(),
CaptchaAction::RENDER_REFRESHED_GET_VAR => 1
));
}
else
{
$url = $this->getController()->createUrl($this->captchaAction, array(
'v' => uniqid()
));
}
$alt = isset($this->imageOptions['alt']) ? $this->imageOptions['alt'] : '';
echo CHtml::image($url, $alt, $this->imageOptions);
}
}
, . , , :
...
$model = new MyModel();
$refreshCaptcha = true;
if (isset($_POST['MyModel']))
{
$refreshCaptcha = false;
...
}
...
$this->render("myView", array(
'model' => $model,
'refreshCaptcha' => $refreshCaptcha
));
captcha myView :
<?php
$this->widget("Captcha", array(
'buttonLabel' => "Generate another code",
'showRefreshButton' => false,
'clickableImage' => true,
'refresh' => $refreshCaptcha
));
actions , CCaptchaAction CaptchaAction:
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CaptchaAction',
'backColor'=>0xFFFFFF
)
);
}
, , . , -.