yii 进入后台 inex.php,PHP应用:Yii中实现处理前后台登录的新方法
《PHP应用:Yii中实现处理前后台登录的新方法》要点:
本文介绍了PHP应用:Yii中实现处理前后台登录的新方法,希望对您有用。如果有疑问,可以联系我们。
相关主题:YII框架
PHP实战因为最近在做一个项目涉及到前后台登录问题,我是把后台作为一个模块(Module)来处理的.我看很多人放两个入口文件index.php和admin.php,然后分别指向前台和后台.这种办法固然很好,可以将前后台完全分离,但我总觉得这种方式有点牵强,这和两个应用啥区别?还不如做两个App用一个framework更好.而且Yii官方后台使用办法也是使用Module的方式.但是Moudle的方式有一个很头疼的问题,就是在使用Cwebuser登录时会出现前后台一起登录一起退出的问题,这显然是不合理的.我纠结了很久才找到下文即将介绍的办法,当然,很多也是参考别人的,自己稍作了改动.我一开始的做法是在后台登录时设置一个isadmin的session,然后再前台登录时注销这个session,这样做只能辨别是前台登录还是后台登录,但做不到前后台一起登录,也即前台登录了后台就退出了,后台登录了前台就退出了.出现这种原因的根本原因是我们使用了同一个Cwebuser实例,不能同时设置前后台session,要解决这个问题就要将前后台使用不同的Cwebuser实例登录.下面是我的做法,首先看protected->config->main.php里对前台user(Cwebuser)的配置:
PHP实战
'user'=>array(
'class'=>'WebUser',//这个WebUser是继承CwebUser,稍后给出它的代码
'stateKeyPrefix'=>'member',//这个是设置前台session的前缀
'allowAutoLogin'=>true,//这里设置允许cookie保存登录信息,一边下次自动登录
),
PHP实战在你用Gii生成一个admin(即后台模块名称)模块时,会在module->admin下生成一个AdminModule.php文件,该类继承了CWebModule类,下面给出这个文件的代码,关键之处就在该文件,望大家仔细研究:
PHP实战
class AdminModule extends CWebModule
{
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
parent::init();//这步是调用main.php里的配置文件
// import the module-level models and componen
$this->setImport(array(
'admin.models.*',
'admin.components.*',
));
//这里重写父类里的组件
//如有需要还可以参考API添加相应组件
Yii::app()->setComponents(array(
'errorHandler'=>array(
'class'=>'CErrorHandler',
'errorAction'=>'admin/default/error',
),
'admin'=>array(
'class'=>'AdminWebUser',//后台登录类实例
'stateKeyPrefix'=>'admin',//后台session前缀
'loginUrl'=>Yii::app()->createUrl('admin/default/login'),
),
), false);
//下面这两行我一直没搞定啥意思,貌似CWebModule里也没generatorPaths属性和findGenerators()办法
//$this->generatorPaths[]='admin.generators';
//$this->controllerMap=$this->findGenerators();
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
$route=$controller->id.'/'.$action->id;
if(!$this->allowIp(Yii::app()->request->userHostAddress) && $route!=='default/error')
throw new CHttpException(403,"You are not allowed to access this page.");
$publicPages=array(
'default/login',
'default/error',
);
if(Yii::app()->admin->isGuest && !in_array($route,$publicPages))
Yii::app()->admin->loginRequired();
else
return true;
}
return false;
}
protected function allowIp($ip)
{
if(empty($this->ipFilters))
return true;
foreach($this->ipFilters as $filter)
{
if($filter==='*' || $filter===$ip || (($pos=strpos($filter,'*'))!==false && !strncmp($ip,$filter,$pos)))
return true;
}
return false;
}
}
?>
PHP实战AdminModule 的init()办法就是给后台配置另外的登录实例,让前后台使用不同的CWebUser,并设置后台session前缀,以便与前台session区别开来(他们同事存在$_SESSION这个数组里,你可以打印出来看看).
PHP实战这样就已经做到了前后台登录分离开了,但是此时你退出的话你就会发现前后台一起退出了.于是我找到了logout()这个办法,发现他有一个参数$destroySession=true,原来如此,如果你只是logout()的话那就会将session全部注销,加一个false参数的话就只会注销当前登录实例的session了,这也就是为什么要设置前后台session前缀的原因了,下面我们看看设置了false参数的logout办法是如何注销session的:
PHP实战
/**
* Clears all user identity information from persistent storage.
* This will remove the data stored via {@link setState}.
*/
public function clearStates()
{
$keys=array_keys($_SESSION);
$prefix=$this->getStateKeyPrefix();
$n=strlen($prefix);
foreach($keys as $key)
{
if(!strncmp($key,$prefix,$n))
unset($_SESSION[$key]);
}
}
PHP实战看到没,就是利用匹配前缀的去注销的.
PHP实战到此,我们就可以做到前后台登录分离,退出分离了.这样才更像一个应用,是吧?嘿嘿…
PHP实战差点忘了说明一下:
PHP实战
Yii::app()->user //前台拜访用户信息方法
Yii::app()->admin //后台拜访用户信息方法
PHP实战不懂的仔细看一下刚才前后台CWebUser的配置.
PHP实战附件1:WebUser.php代码:
PHP实战
class WebUser extends CWebUser
{
public function __get($name)
{
if ($this->hasState('__userInfo')) {
$user=$this->getState('__userInfo',array());
if (isset($user[$name])) {
return $user[$name];
}
}
return parent::__get($name);
}
public function login($identity, $duration) {
$this->setState('__userInfo', $identity->getUser());
parent::login($identity, $duration);
}
}
?>
PHP实战附件2:AdminWebUser.php代码
PHP实战
class AdminWebUser extends CWebUser
{
public function __get($name)
{
if ($this->hasState('__adminInfo')) {
$user=$this->getState('__adminInfo',array());
if (isset($user[$name])) {
return $user[$name];
}
}
return parent::__get($name);
}
public function login($identity, $duration) {
$this->setState('__adminInfo', $identity->getUser());
parent::login($identity, $duration);
}
}
?>
PHP实战附件3:前台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 $user;
public $_id;
public $username;
public function authenticate()
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
$user=User::model()->find('username=:username',array(':username'=>$this->username));
if ($user)
{
$encrypted_passwd=trim($user->password);
$inputpassword = trim(md5($this->password));
if($inputpassword===$encrypted_passwd)
{
$this->errorCode=self::ERROR_NONE;
$this->setUser($user);
$this->_id=$user->id;
$this->username=$user->username;
//if(isset(Yii::app()->user->thisisadmin))
// unset (Yii::app()->user->thisisadmin);
}
else
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
}
else
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
unset($user);
return !$this->errorCode;
}
public function getUser()
{
return $this->user;
}
public function getId()
{
return $this->_id;
}
public function getUserName()
{
return $this->username;
}
public function setUser(CActiveRecord $user)
{
$this->user=$user->attributes;
}
}
PHP实战附件4:后台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 $admin;
public $_id;
public $username;
public function authenticate()
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
$user=Staff::model()->find('username=:username',array(':username'=>$this->username));
if ($user)
{
$encrypted_passwd=trim($user->password);
$inputpassword = trim(md5($this->password));
if($inputpassword===$encrypted_passwd)
{
$this->errorCode=self::ERROR_NONE;
$this->setUser($user);
$this->_id=$user->id;
$this->username=$user->username;
// Yii::app()->user->setState("thisisadmin", "true");
}
else
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
}
else
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
unset($user);
return !$this->errorCode;
}
public function getUser()
{
return $this->admin;
}
public function getId()
{
return $this->_id;
}
public function getUserName()
{
return $this->username;
}
public function setUser(CActiveRecord $user)
{
$this->admin=$user->attributes;
}
}
PHP实战希望本文所述对大家基于Yii框架的PHP程序设计有所赞助.
维易PHP培训学院每天发布《PHP应用:Yii中实现处理前后台登录的新方法》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。
yii 进入后台 inex.php,PHP应用:Yii中实现处理前后台登录的新方法相关推荐
- 使用layui 做后台管理界面,在Tab中的链接点击后添加一个新TAB的解决方法
使用layui 做后台管理界面,在Tab中的链接点击后添加一个新TAB的解决方法 参考文章: (1)使用layui 做后台管理界面,在Tab中的链接点击后添加一个新TAB的解决方法 (2)https: ...
- php yii框架连接数据库,【PHP开发框架】yii框架怎样衔接数据库
yii框架设置数据库衔接 入手下手之前,请确保你已装置了 PHP PDO 扩大和你所运用的数据库的 PDO 驱动(比方 MySQL 的 pdo_mysql). 关于运用关联型数据库来说,这是基本要求. ...
- 二、yii的入口与app应用实例(Yii::app())
为什么80%的码农都做不了架构师?>>> 那么在浏览器中输入 localhost/bootstrap/index.php时 yii都做为我们做了一些什么咧 首先 我们来看ind ...
- php博客系统 加载评论,Yii实现单用户博客系统文章详情页插入评论表单的方法...
本文实例讲述了Yii实现单用户博客系统文章详情页插入评论表单的方法.分享给大家供大家参考,具体如下: action部分: function test($objs) { $objs->var=10 ...
- yii框架如何连接mysql数据库_php yii框架怎么连接数据库
这篇文章主要介绍了PHP的Yii框架中使用数据库的配置和SQL操作实例教程,Yii支持绑定多种数据库,文中主要对各种查询语句在Yii中的使用给出了例子,需要的朋友可以参考下 php yii框架怎么连接 ...
- php中yii的controller,详解PHP的Yii框架中的Controller控制器,yiicontroller
详解PHP的Yii框架中的Controller控制器,yiicontroller 控制器是 MVC 模式中的一部分, 是继承yii\base\Controller类的对象,负责处理请求和生成响应. 具 ...
- php+yii框架,【Yii框架 1 】PHP框架,Yii概述
[Yii框架 一 ]PHP框架,Yii概述 PHP的框架,只接触过三种:ThinkPHP.Laravel.Yii,前两种作了解,项目中用的Yii. 一.3种框架简述 1. ThinkPHP Think ...
- java取list中最大数值_Java后台通过Collections获取list集合中最大数,最小数代码
我就废话不多说了,大家还是直接看代码吧~ package com.jalor; import java.util.ArrayList; import java.util.Collections; im ...
- jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法
jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 参考文章: (1)jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 ( ...
- node后台fetch请求数据-Hostname/IP doesn‘t match certificate‘s altnames解决方法
node后台fetch请求数据-Hostname/IP doesn't match certificate's altnames解决方法 参考文章: (1)node后台fetch请求数据-Hostna ...
最新文章
- 《数学之美》第18章 闪光的不一定是金子--谈谈搜索引擎反作弊问题和搜索结果的权威性问题
- Deeplearning.ai深度学习课程笔记-在线版
- TypeScript 3.9 正式发布!平均编译时长从 26 秒缩短至 10 秒
- MS Lync2010客户端开发体会
- Python数据结构常见的八大排序算法(详细整理)
- C#调用带结构体指针的C Dll的方法
- 信息学奥赛一本通 2048:【例5.18】串排序
- CAN笔记(2) CAN特点
- NameError: name 'random' is not defined
- mysql mac版本_mysql的安装、启动和基础配置 —— mac版本
- TensorFlow.js 来了!浏览器上线机器学习功能
- html动画效果效果图,利用CSS3实现雷达扫描效果图动画特效
- 禁用/开启 Windows系统3D加速
- 【PE】PE文件结构学习
- html页面顶部横条,CSS:页面顶部的精简横幅(如本页中的橙色横幅)
- 派森编程python_Python(派森)
- android更改app背景颜色,使用AppCompat更改操作栏的背景颜色
- C++中的stack容器适配器
- 免费的DNS服务OpenDNS、Google Public DNS
- 递推算法--昆虫繁殖
热门文章
- 渗透测试常见问题以及方法
- HTML+CSS个人静态网页设计
- 夜神模拟器安装激活面具magisk教程
- Linux中的数学运算
- oracle 计算时间差 毫秒,Oracle计算时间差为毫秒的实现代码
- w ndoWs8pE模式下载,天意PE迷你版V2011.9.9(天意PE系统)下载 - 下载吧
- python的logo的代码_简单几步,100行代码用Python画一个蝙蝠侠的logo
- 电脑连不上网络,报红叉,网络适配器报黄色警告,错误代码56,解决办法
- 行人属性数据集pa100k_澎思科技荣登「2019 AI 最佳掘金案例年度榜单」,视频结构化技术助力AI产业化落地...
- M1芯片安装CleanMyMac X4.7.4的方法(附下载)M1芯片安装那个CleanMyMac X版本?CleanMyMac X已完美支持M1芯片安装 支持big sur系统