在Yii中使用Captcha验证码
来源:互联网 发布:php动态网页设计代码 编辑:程序博客网 时间:2024/05/16 05:40
在Yii中使用Captcha验证码:
详细代码请参考:yii自带的示例代码post 项目,里面有一个contact表单用到了验证码.
1,Model:
将验证码加入UserLogin的一个属性:
class UserLogin extends CFormModel{public $username;public $password;public $rememberMe;public $verifyCode;public function rules(){return array(// username and password are requiredarray('username, password,verifyCode', 'required'),// rememberMe needs to be a booleanarray('rememberMe', 'boolean'),// password needs to be authenticatedarray('password', 'authenticate'),// verifyCode needs to be entered correctlyarray('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),);}/** * Declares attribute labels. */public function attributeLabels(){return array('rememberMe'=>Yii::t('user',"Remember me next time"),'username'=>Yii::t('user',"username or email"),'password'=>Yii::t('user',"password"),'verifyCode'=>Yii::t('user','Verification Code'),);}
2,Controller
在LoginController控制器加入映射动作CCaptchaAction
public function actions(){return array(// captcha action renders the CAPTCHA image displayed on the contact page'captcha'=>array('class'=>'CCaptchaAction','backColor'=>0xf4f4f4,'padding'=>0,'height'=>30,'maxLength'=>4,),);}public function actionLogin(){if (Yii::app()->user->isGuest) {$model=new UserLogin;// collect user input dataif(isset($_POST['UserLogin'])){$model->attributes=$_POST['UserLogin'];//在此核对验证码if($this->createAction('captcha')->validate($model->verifyCode, false)){// validate user input and redirect to previous page if validif($model->validate()) {//admin login onlyif( Yii::app()->getModule('user')->isAdmin()==1 ){$this->lastViset();if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)$this->redirect(Yii::app()->controller->module->returnUrl);else$this->redirect(Yii::app()->user->returnUrl);}else{//if no admin when login out$this->redirect(Yii::app()->controller->module->logoutUrl);}}}else{//提示错误$model->addError('verifyCode','验证码不对');}}// display the login form$this->render('/user/login',array('model'=>$model));} else$this->redirect(Yii::app()->controller->module->returnUrl);}
在验证用户名密码前,检查验证码:
if($this->createAction('captcha')->validate($model->verifyCode, false)){
3,view
在视图中显示验证码图片,输入框
<?php $this->widget('CCaptcha'); ?><?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?>
---------------------------the end------------------------------------
- 在Yii中使用Captcha验证码
- 在Yii中使用Captcha验证码
- Yii 验证码(captcha)的使用
- Yii框架captcha验证码的使用
- yii验证码Captcha使用以及为什么验证码不刷新问题
- 使用BotDetect Captcha在SpringMCV项目中生成验证码图片
- Cakephp中使用Captcha实现更加安全的验证码
- Yii2.x 中captcha 验证码使用的一些心得
- 关于Yii 验证码(captcha)的一些…
- Javaweb中快速生成验证码Captcha
- 关于Captcha验证码组件的使用
- Yii Framework中验证码的使用
- YII- FRAMEWORK- 中验证码的使用
- 在spring MVC中引入captcha的插件生成图片验证码
- 验证码(CAPTCHA)
- captcha验证码入门
- 验证码 Captcha
- Go实战--golang中使用图片和验证码(dchest/captcha)
- 我的.conkyrc
- 重启及关机
- 权重 相等大于N的最小数
- 你知道的东西
- 信号量
- 在Yii中使用Captcha验证码
- Linux高端内存映射(中)
- 判断浏览器版本及什么浏览器
- Windows服务编写原理及探讨
- 矩阵棋盘划分的CUDA程序
- 一些不错的技术网站
- 俄籍大提琴手辱骂中国女乘客道歉被指无诚意
- mysql 完整的存储过程
- 给做假网站的提个醒——我承认我无聊了