本篇文章是对在yii中新增一个用户验证的方法进行了详细的分析介绍,需要的朋友参考下
1.为什么要新增一个用户验证:
复制代码 代码如下:
新版本程序更新主要体现在:完美整合BBS论坛程序,用户只须注册一个帐号,即可全站通用!采用目前流行的Flash滚动切换广告 变换形式多样,受人喜爱!在原有提供的5种在线支付基础上增加北京云网支付!对留言本重新进行编排,加入留言验证码,后台有留言审核开关对购物系统的前台进行了一处安全更新。在原有文字友情链接基础上,增加LOGO友情链接功能强大的6种在线支付方式可选,自由切换。对新闻列表进行了调整,
0
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
复制代码 代码如下:
// 使用提供的用户名和密码登录用户
$identity=new UserIdentity($username,$password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;
复制代码 代码如下:
// 注销当前用户
Yii::app()->user->logout();
其中的user是yii的一个components.需要在protected/config/main.php中定义
复制代码 代码如下:
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
复制代码 代码如下:
class AdminDefaultController extends CController
{
public function filters()
{
return array('accessControl');
}
public function accessRules()
{
return array(
array(
'allow',
'users' => array('@'),
),
array(
'deny',
'users' => array('*')
),
);
}
}
复制代码 代码如下:
public function filterAccessControl($filterChain)
{
$filter=new CAccessControlFilter;
$filter->setRules($this->accessRules());
$filter->filter($filterChain);
}
复制代码 代码如下:
public function accessRules()
{
return array(
array('deny',
'actions'=>array('create', 'edit'),
'users'=>array('?'),
),
array('allow',
'actions'=>array('delete'),
'roles'=>array('admin'),
),
array('deny',
'actions'=>array('delete'),
'users'=>array('*'),
),
);
}
复制代码 代码如下:
class CAdminWebUser extends CWebUser
{
public $loginUrl = array('admin/admin/login');
}
复制代码 代码如下:
'user'=>array(
// enable cookie-based authentication
'class' => 'CAdminUser',
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
复制代码 代码如下:
$this->setComponents(array(
'adminUser' => array(
'class' => 'CAdminWebUser',
'allowAutoLogin' => false,
)
));
复制代码 代码如下:
//全局应用
Yii::app()->getComponent('adminUser');
//在模块中
Yii::app()->controller->module->getComponent('adminUser');
复制代码 代码如下:
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号