yii
使用captcha验证码的方法,yiicaptcha验证码
本文实例讲述了yii使用captcha验证码的方法。分享给大家供大家参考,具体如下:
详细代码可参考:yii自带的示例代码post项目,里面有一个contact表单用到了验证码.
1. Model:
将验证码加入UserLogin的一个属性:
2. Controller
立即学习
“
PHP免费学习笔记(深入)
”;
WOC-YII开源站群管理系统1.3
WOC-YII是rschome.com基于yii framework 1.1.8框架所开发的一款开源简易站群管理系统。它的功能与WOC完全一样。目前版本为V1.3,新版本正在开发中,同时欢迎大家参与到开发中来! WOC-YII 1.3在1.2的基础上优化了登录系统(密码加密),优化了权限控制系统,新增seo管理功能,新增自动安装向导! 程序框架:yiiframework1.1.8 配置文件:p
下载
在LoginController控制器加入映射动作CCaptchaAction
在验证用户名密码前,检查验证码:
3. view
在视图中显示验证码图片,输入框
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
endmain
您可能感兴趣的文章:
yii用户注册表单验证实例
PHP Yii框架之表单验证规则大全
Yii框架form表单用法实例
Yii不依赖Model的表单生成器用法实例
yii框架表单模型使用及以数组形式提交表单数据示例
Yii中实现处理前后台登录的新方法
yii去掉必填项中星号的方法
http://www.bkjia.com/PHPjc/1085886.html
www.bkjia.com
true
http://www.bkjia.com/PHPjc/1085886.html
TechArticle
Yii使用Captcha验证码的方法,yiicaptcha验证码 本文实例讲述了Yii使用Captcha验证码的方法。分享给大家供大家参考,具体如下: 详细代码可参...
class UserLogin extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $verifyCode;
public function rules()
{
return array(
// username and password are required
array('username, password,verifyCode', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
// verifyCode needs to be entered correctly
array('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'),
);
}
}
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,
),
);
}
ublic function actionLogin()
{
if (Yii::app()->user->isGuest) {
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
//在此核对验证码
if($this->createAction('captcha')->validate($model->verifyCode, false))
{
// validate user input and redirect to previous page if valid
if($model->validate()) {
//admin login only
if( 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))
{
<?php $this->widget('CCaptcha'); ?>
<?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?>
@@##@@
