小编是特别的爱学习 然后费劲的自己做了一整天终于整出来了,就是验证用户名和密码有些问题 ,但是图片验证是没问题的 用户名 密码是我自己验证的 ,接下来请看详细的代码 ,有更好的可以告诉我,评论下,谢谢!!!!!
控制器LoginController.PHP
<?php namespace backend\controllers; use Yii; use yii\debug\models\search\Log; use yii\web\Controller; use backend\models\Login; use frontend\models\ContactForm; use Illuminate\Http\Request; use yii\web\Session; use backend\controllers\CommonController; class LoginController extends Controller { public function actions(){ return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'maxLength'=>4, 'minLength'=>4, ], ]; } public function actionIndex(){ //调用model $model = new Login(); if ($model->load(Yii::$app->request->post()) && $model->validate()){ return $this->refresh(); } //我的view里面是login,所以下面是login return $this->render('login',[ 'model'=>$model, ]); }
}
model里面Login.php
<?php namespace backend\models; use Yii; use yii\base\Model; use yii\db\Query; use common\models\User; class Login extends Model { public $verifyCode; public $username; public $password; private $_user; /** * @return array the validation rules. */ public function rules() { return [ // [['username'], 'required','message'=>'用户名不能为空'], [[ 'password'], 'required','message'=>'密码不能为空'], // verifyCode needs to be entered correctly ['verifyCode', 'required','message'=>'验证码不能为空'], ['verifyCode', 'captcha','message'=>'验证码不正确'], ['username', 'unique', 'targetClass' => '\frontend\models\Login', 'message' => '用户名已存在.'], ]; } /** * @return array customized attribute labels */ public function attributeLabels() { return [ 'rememberMe'=>'下次记住我', 'verifyCode' =>'' ]; } /** * @param $verifyCode * @return bool */ public function validateVerifyCode($verifyCode){ if(strtolower($this->verifyCode) === strtolower($verifyCode)){ return true; }else{ $this->addError('verifyCode','验证码错误.'); } } /** * @param $username * @param $password * @return int|string * 验证用户名,密码返回C层 */ public function code($username,$password){ $sql="select * from admin where admin_name='$username'"; $db =\Yii::$app->db->createCommand($sql)->queryOne(); if($db){ if($db['admin_pwd']==md5($password)){ return 0; }else{ return "-1"; } }else{ return "1"; } } } view里面login就这样 yii2验证码就可以使用了<?php use common\widgets; use yii\helpers\Html; use yii\bootstrap\ActiveForm; use yii\captcha\Captcha; use yii\helpers\Url; use yii\widgets\LinkPager; use yii\base; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> <link rel="stylesheet" href="css/reset.css" /> <link rel="stylesheet" href="css/login.css" /> </head> <body> <div class="page"> <div class="loginwarrp"> <div class="logo">管理员登陆</div> <div class="login_form"> <?php $form=ActiveForm::begin([ 'action'=>Url::toRoute(['show']), 'method'=>'post', ]); ?> <th>姓名:</th><input type="text" name="username" style="width: 206px;height: 32px"><br><br> <th>密码:</th><input type="password" name="password" style="width: 206px;height: 32px"><br><br> <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 'template' => '<div class="row"><div class="col-lg-4">{image}</div><div class="col-lg-6">{input}</div></div>', ]) ?> <div class="form-group"> <?= Html::submitButton('登录一下', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div> </div> </div> </body> </html>在下面还需要改yii2里面原生代码,想知道这是为什么吗???因为原生代码是site/captcha,应该是两个php页面有
vendor/yiisoft/yii2/captcha/
Captcha.php 还有一个我给忘记了 就在这个文件里captcha网上的大多数解决方法都是通过修改
vendor/yiisoft/yii2/captcha/CaptchaAction.php
中的代码来解决,以下两种方法可以任选其一:1.修改getVerifyCode()方法的参数默认值
将参数$regenerate的默认值由false改为true,这样在不传参数的情况下,程序每次获取验证码时都会重新生成。2.修改run()方法
在红色箭头指向的地方中,添加一个参数true,同样可以解决问题。带来的问题
使用上面两种方法确实都可以解决验证码不刷新的问题,但这样会带来一个新的问题,就是在开启表单客户端验证(enableClientValidation)的情况下,即使用户输入了正确的验证码,网页仍然会提示“验证码错误”:
那肿么办呢?在这里我们可以选择关闭表单的客户端验证功能,以此来解决这个问题:
但这种解决方法不是很完美,因为关闭了客户端的验证功能后,表单数据就只能提交到后台后再验证了,这样无疑会增加服务器的压力。完美解决方法
上面说了那么多,解决方法貌似都不是很完美,其实要完美解决验证码不刷新的问题十分简单,我们不需要修改
CaptchaAction.php
,只要修改vendor/yiisoft/yii2/captcha/CaptchaValidator.php
这个文件就可以了,修改的地方见下图:
在红色箭头指向的地方,将参数false改为true即可完美解决问题。总结
归根到底,是因为yii2在渲染Captcha小部件的时候,会先输出js验证代码,然后再渲染验证码图片,也就是说,验证码字符串必须在输出js代码前就要重新生成,而
CaptchaAction.php
中的run()
方法是渲染验证码图片的时候调用的,CaptchaValidator.php
中的clientValidateAttribute()
是输出js代码的时候调用的,所以接下来的道理,相信大家都已经明白了。