php - Yii 验证码没有改变

标签 php yii

我的验证码没有改变,总是出现相同的单词,除非点击重新加载验证码按钮。为什么 testLimit 无法正常工作?

Controller .php

public $attempts = 5; // allowed 5 attempts
public $counter;

public function actions()
{
    return array(
        'captcha'=>array(
        'class'=>'CCaptchaAction',
        'backColor'=>0xf5f5f5,
        'testLimit'=>1,
    );
}

private function captchaRequired()
{           
    return Yii::app()->session->itemAt('captchaRequired') >= $this->attempts;
}

public function actionLogin()
{
    if (!Yii::app()->user->isGuest) $this->redirect(array('users/update'));

    $model = $this->captchaRequired()? new LoginForm('captchaRequired') : new LoginForm;

    // collect user input data
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login()) {
            $this->redirect(array('users/update'));
        } else {
            $this->counter = Yii::app()->session->itemAt('captchaRequired') + 1;
            Yii::app()->session->add('captchaRequired',$this->counter);
        }
    }
    // display the login form
    $this->render('login',array('model'=>$model));
}

查看.php

<?php if($model->scenario == 'captchaRequired'): ?>
    <br>
    <legend><?php echo CHtml::activeLabelEx($model,'verifyCode'); ?></legend>
    <div class="control-group">
        <div class="controls">
            <?php $this->widget('CCaptcha'); ?>
            <?php echo CHtml::activeTextField($model,'verifyCode'); ?>
        </div>
    </div>
<?php endif; ?>

最佳答案

testLimit 是验证码提交的数量,用户可以在生成的哈希更改之前尝试。用于避免拼写错误。

验证 session 中的代码存储 ( http://www.yiiframework.com/doc/api/1.1/CCaptchaAction#getVerifyCode-detail ),因此默认代码只能通过以下两种方式之一进行更改:使用错误代码提交带有 testLimit 次的表单,或由用户手动更新。

因此您可以扩展 CCaptchaAction 类来实现您想要的,例如强制将 $regenerate 变量设置为 true。

关于php - Yii 验证码没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567482/

相关文章:

php - MySQL 过程(帮助)

javascript - 如何在用户删除之前添加确认消息?

php - 如何通过计算在一个查询中更新数据库中的多行

php - fatal error : Call to a member function saveAs() on a non-object on FileField Update

php - Yii 修改管理员密码

php - 文件上传 : Size validator or setMaxFileSize()

php - 从非对象调用方法?

mysql - 交易在 Yii 中不起作用

php - 当为 php yiiframework 上传新图像时从文件夹中删除图像

json - YII-在表单验证中添加错误代码