Yii框架实现的验证码、登录及退出功能示例
发布时间 - 2026-01-11 01:11:56 点击率:次本文实例讲述了Yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下:

捣鼓了一下午,总算走通了,下面贴出代码。
Model
<?php
class Auth extends CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{auth}}';
}
}
注:我的用户表是auth,所以模型是Auth.php
<?php
class IndexForm extends CFormModel {
public $a_account;
public $a_password;
public $rememberMe;
public $verifyCode;
public $_identity;
public function rules() {
return array(
array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message'=>'请输入正确的验证码'),
array('a_account', 'required', 'message' => '用户名必填'),
array('a_password', 'required', 'message' => '密码必填'),
array('a_password', 'authenticate'),
array('rememberMe', 'boolean'),
);
}
public function authenticate($attribute, $params) {
if (!$this->hasErrors()) {
$this->_identity = new UserIdentity($this->a_account, $this->a_password);
if (!$this->_identity->authenticate()) {
$this->addError('a_password', '用户名或密码不存在');
}
}
}
public function login() {
if ($this->_identity === null) {
$this->_identity = new UserIdentity($this->a_account, $this->a_password);
$this->_identity->authenticate();
}
if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
$duration = $this->rememberMe ? 60*60*24*7 : 0;
Yii::app()->user->login($this->_identity, $duration);
return true;
} else {
return false;
}
}
public function attributeLabels() {
return array(
'a_account' => '用户名',
'a_password' => '密码',
'rememberMe' => '记住登录状态',
'verifyCode' => '验证码'
);
}
}
注:IndexForm也可以写成LoginForm,只是系统内已经有了,我就没有替换它,同时注意看自己用户表的字段,一般是password和username,而我的是a_account和a_password
Controller
<?php
class IndexController extends Controller {
public function actions() {
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'width'=>100,
'height'=>50
)
);
}
public function actionLogin() {
if (Yii::app()->user->id) {
echo "<div>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";
} else {
$model = new IndexForm();
if (isset($_POST['IndexForm'])) {
$model->attributes = $_POST['IndexForm'];
if ($model->validate() && $model->login()) {
echo "<div>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";exit;
}
}
$this->render('login', array('model' => $model));
}
}
public function actionLogout() {
Yii::app()->user->logout();
$this->redirect(SITE_URL . 'admin/index/login');
}
}
注:第一个方法是添加验证码的
view
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'login-form',
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true
)
));
?>
<div class="row">
<?php echo $form->labelEx($model,'a_account'); ?>
<?php echo $form->textField($model,'a_account'); ?>
<?php echo $form->error($model,'a_account'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'a_password'); ?>
<?php echo $form->passwordField($model,'a_password'); ?>
<?php echo $form->error($model,'a_password'); ?>
</div>
<?php if(CCaptcha::checkRequirements()) { ?>
<div class="row">
<?php echo $form->labelEx($model, 'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model, 'verifyCode'); ?>
<?php echo $form->error($model, 'verifyCode'); ?>
</div>
<?php } ?>
<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('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
同时修改项目下protected/components下的UserIdentity.php
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
/*
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
*/
$user_model = Auth::model()->find('a_account=:name',array(':name'=>$this->username));
if($user_model === null){
$this -> errorCode = self::ERROR_USERNAME_INVALID;
return false;
} else if ($user_model->a_password !== md5($this -> password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
return false;
} else {
$this->errorCode=self::ERROR_NONE;
return true;
}
}
}
更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
# Yii框架
# 验证码
# 登录
# 退出
# Yii2框架实现登陆添加验证码功能示例
# Yii2 如何在modules中添加验证码的方法
# Yii2下点击验证码的切换实例代码
# Yii2简单实现给表单添加验证码的方法
# Yii2增加验证码步骤详解
# yii2中添加验证码的实现方法
# Yii1.0 不同页面多个验证码的使用实现
# Yii 2.0自带的验证码使用经验分享
# Yii输入正确验证码却验证失败的解决方法
# Yii使用Captcha验证码的方法
# yii实现创建验证码实例解析
# YII2框架中验证码的简单使用方法示例
# 程序设计
# 必填
# 的是
# 我就
# 相关内容
# 第一个
# 感兴趣
# 给大家
# 请输入
# 而我
# 不存在
# 已经有了
# 更多关于
# 所述
# 贴出
# 面向对象
# 操作技巧
# 下午
# 讲述了
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
如何在万网利用已有域名快速建站?
潮流网站制作头像软件下载,适合母子的网名有哪些?
Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议
公司网站制作需要多少钱,找人做公司网站需要多少钱?
DeepSeek是免费使用的吗 DeepSeek收费模式与Pro版本功能详解
javascript中闭包概念与用法深入理解
JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)
青岛网站建设如何选择本地服务器?
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能
如何选择PHP开源工具快速搭建网站?
Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】
Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】
如何在景安云服务器上绑定域名并配置虚拟主机?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
Laravel如何实现一对一模型关联?(Eloquent示例)
高防服务器租用首荐平台,企业级优惠套餐快速部署
HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】
非常酷的网站设计制作软件,酷培ai教育官方网站?
java中使用zxing批量生成二维码立牌
如何获取上海专业网站定制建站电话?
如何挑选优质建站一级代理提升网站排名?
百度浏览器如何管理插件 百度浏览器插件管理方法
Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】
laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法
Android自定义listview布局实现上拉加载下拉刷新功能
Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程
如何在阿里云高效完成企业建站全流程?
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
如何在阿里云完成域名注册与建站?
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
js实现点击每个li节点,都弹出其文本值及修改
如何在企业微信快速生成手机电脑官网?
通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】
iOS UIView常见属性方法小结
Android滚轮选择时间控件使用详解
手机软键盘弹出时影响布局的解决方法
北京企业网站设计制作公司,北京铁路集团官方网站?
图册素材网站设计制作软件,图册的导出方式有几种?
如何确保FTP站点访问权限与数据传输安全?
Laravel Eloquent关联是什么_Laravel模型一对一与一对多关系精讲
Laravel怎么实现微信登录_Laravel Socialite第三方登录集成
网站建设要注意的标准 促进网站用户好感度!
Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】
如何快速生成高效建站系统源代码?
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
Swift中循环语句中的转移语句 break 和 continue
简历在线制作网站免费版,如何创建个人简历?
如何制作一个表白网站视频,关于勇敢表白的小标题?
canvas 画布在主流浏览器中的尺寸限制详细介绍

