17-05-1394، 11:25 ق.ظ
من لاگین رو به صفحه ایندکس سایتم آوردم
این کد های من هست ، اگه دفعه اول وارد بشم ( که نیازی به کپتچا نداره ) به صورت صحیح وارد میشه
اگه نام کاربری یا کلمه عبور اشتباه وارد بشه کپتچا نمایان میشه ، از زمانی که کپتچا ظاهر میشه ، در صورتی که نام کاربری کلمه عبور و کپیچا صحیح هم باشه ، پیغام خطا کپتچا صحیح نیست ظاهر میشه. و لاگین صورت نمیگیره .
loginForm
<?php /** * LoginForm class. * LoginForm is the data structure for keeping * user login form data. It is used by the 'login' action of 'SiteController'. */ class LoginForm extends CFormModel { public $username; public $password; public $rememberMe; public $verifyCode; private $_identity; /** * Declares the validation rules. * The rules state that username and password are required, * and password needs to be authenticated. */ public function rules() { return array( // username and password are required array('username, password', 'required'), // rememberMe needs to be a boolean array('rememberMe', 'boolean'), // password needs to be authenticated array('password', 'authenticate'), // array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on' => 'captchaNeed'), ); } /** * Declares attribute labels. */ public function attributeLabels() { return array( 'rememberMe'=>'Remember me next time', ); } /** * Authenticates the password. * This is the 'authenticate' validator as declared in rules(). */ public function authenticate($attribute,$params) { if(!$this->hasErrors()) { $this->_identity=new UserIdentity($this->username,$this->password); if(!$this->_identity->authenticate()) $this->addError('password','Incorrect username or password.'); } } /** * Logs in the user using the given username and password in the model. * @return boolean whether login is successful */ public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; } }
site/index
/** * This is the default 'index' action that is invoked * when ad action is not explicitly requested by users. */ public function actionIndex(){ // set default layout $this->layout = 'main'; $loginModel = new LoginForm; if(Yii::app()->request->isAjaxRequest) { if(isset(Yii::app()->session['captchaNeed'])) LoginModel->scenario = 'captchaNeed'; if(isset($_POST['LoginForm'])) { $loginModel->attributes = $_POST['LoginForm']; if($loginModel->validate() && $loginModel->login()) { echo '<script>window.location= "' . Yii::app()->createUrl('site/dashboard') . '"</script>'; unset(Yii::app()->session['captchaNeed']); Yii::app()->end(); }else{ Yii::app()->session['captchaNeed'] = true; $loginModel->scenario = 'captchaNeed'; } $this->renderPartial('_login', array('model' => $loginModel)); } } if(!Yii::app()->request->isAjaxRequest) $this->render('index', array('loginModel' => $loginModel)); }
siteController --- actions()
public function actions() { return array( 'captcha'=>array( 'class'=>'CCaptchaAction', 'minLenght'=>4, 'maxLenght'=>4, 'testLimit'=>1, 'backColor'=>0x7D287E, 'foreColor'=>0xFFFFFF, 'height'=>24, 'width'=>100, 'offset'=>1, ), ) }
viewsiteindex
<section class="col-xs-8" id="index-login"> <?php if(Yii::app()->user->isGuest) : ?> <div id="login"> <?php $this->renderPartial('_login', array('model' => $loginModel)) ; ?> </div> <?php echo CHtml::ajaxSubmitButton('Login', Yii::app()->createUrl('site/index'), array( 'type' => 'post', 'update' => '#login', 'data' => 'js:$("#login-form").serialize(); ), array( 'id'=>uniqid(), 'class'=>'btn btn-danger', ) ); ?> <?php endif; ?> </section> </div> </div>
viewsite_login
<?php /* @var $this SiteController */ /* @var $model LoginForm */ /* @var $form CActiveForm */ $this->pageTitle=Yii::app()->name . ' - Login'; $this->breadcrumbs=array( 'Login', ); ?> <h1>Login</h1> <p>Please fill out the following form with your login credentials:</p> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'login-form', )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <div class="row"> <?php echo $form->labelEx($model,'username'); ?> <?php echo $form->textField($model,'username'); ?> <?php echo $form->error($model,'username'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'password'); ?> <?php echo $form->passwordField($model,'password'); ?> <?php echo $form->error($model,'password'); ?> <p class="hint"> Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>. </p> </div> <?php if(CCaptcha::checkRequirements() && $model->scenario == 'captchaNeed'): ?> <div class="row"> <?php echo $form->labelEx($model,'verifyCode'); ?> <div> <?php $this->widget('CCaptcha'); ?> <?php echo $form->textField($model,'verifyCode'); ?> </div> <div class="hint">Please enter the letters as they are shown in the image above. <br/>Letters are not case-sensitive.</div> <?php echo $form->error($model,'verifyCode'); ?> </div> <?php endif; ?> <div class="row rememberMe"> <?php echo $form->checkBox($model,'rememberMe'); ?> <?php echo $form->label($model,'rememberMe'); ?> <?php echo $form->error($model,'rememberMe'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('Login'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->