關於Yii中處理前後台登入的方法實現
這篇文章主要介紹了Yii中實現處理前後台登入的新方法,具體分析了Yii中前後台登入的新思路與相關實現技巧,需要的朋友可以參考下
本文實例講述了Yii中實作處理前後台登入的新方法。分享給大家供大家參考,具體如下:
因為最近在做一個專案涉及到前後台登入問題,我是把後台當作一個模組(Module)來處理的。我看很多人放兩個入口文件index.php和admin.php,然後分別指前台和後台。這種方法固然很好,可以將前後台完全分離,但我總覺得這種方式有點牽強,這和兩個應用啥區別?還不如做兩個App用一個framework更好。而Yii官方後台使用方法也是使用Module的方式。但是Moudle的方式有一個很頭痛的問題,就是在使用Cwebuser登入時會出現前後台一起登入一起登出的問題,這顯然是不合理的。我糾結了很久才找到下文即將介紹的方法,當然,很多也是參考別人的,自己稍作了改動。我一開始的做法是在後台登入時設定一個isadmin的session,然後再前台登入時註銷這個session,這樣做只能辨別是前台登入還是後台登錄,但做不到前後台一起登錄,也即前台登入了後台就登場了,後台登入了前台就退場了。造成這種原因的根本原因是我們使用了同一個Cwebuser實例,不能同時設定前後台session,要解決這個問題就要將前後台使用不同的Cwebuser實例登入。以下是我的做法,首先看protected->config->main.php裡對前台user(Cwebuser)的設定:
'user'=>array( 'class'=>'WebUser',//这个WebUser是继承CwebUser,稍后给出它的代码 'stateKeyPrefix'=>'member',//这个是设置前台session的前缀 'allowAutoLogin'=>true,//这里设置允许cookie保存登录信息,一边下次自动登录 ),
<?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; } } ?>
/** * 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]); } }
Yii::app()->user //前台访问用户信息方法 Yii::app()->admin //后台访问用户信息方法
#附件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); } } ?>
附件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); } } ?>
附件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; } }
##附件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中文網!
相關推薦:
關於Yii2中使用join和joinwith進行多表關聯查詢
#########關於利用Yii2微信後台開發的解析########################以上是關於Yii中處理前後台登入的方法實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在當前資訊時代,大數據、人工智慧、雲端運算等技術已成為了各大企業關注的熱點。在這些技術中,顯示卡渲染技術作為一種高效能圖形處理技術,受到了越來越多的關注。顯示卡渲染技術廣泛應用於遊戲開發、影視特效、工程建模等領域。而對於開發者來說,選擇一個適合自己專案的框架,是一個非常重要的決策。在目前的語言中,PHP是一種相當有活力的語言,一些優秀的PHP框架如Yii2、Ph

隨著雲端運算技術的不斷發展,資料的備份已經成為了每個企業必須要做的事情。在這樣的背景下,開發一款高可用的雲端備份系統尤其重要。而PHP框架Yii是一款功能強大的框架,可以幫助開發者快速建立高效能的Web應用程式。以下將介紹如何使用Yii框架開發一款高可用的雲端備份系統。設計資料庫模型在Yii框架中,資料庫模型是非常重要的一環。因為資料備份系統需要用到很多的表和關

隨著互聯網的不斷發展,Web應用程式開發的需求也越來越高。對於開發人員而言,開發應用程式需要一個穩定、高效、強大的框架,這樣可以提高開發效率。 Yii是一款領先的高效能PHP框架,它提供了豐富的特性和良好的性能。 Yii3是Yii框架的下一代版本,它在Yii2的基礎上進一步優化了效能和程式碼品質。在這篇文章中,我們將介紹如何使用Yii3框架來開發PHP應用程式。

Yii框架是一個開源的PHPWeb應用程式框架,提供了眾多的工具和元件,簡化了Web應用程式開發的流程,其中資料查詢是其中一個重要的元件之一。在Yii框架中,我們可以使用類似SQL的語法來存取資料庫,從而有效率地查詢和操作資料。 Yii框架的查詢建構器主要包括以下幾種類型:ActiveRecord查詢、QueryBuilder查詢、命令查詢和原始SQL查詢

隨著Web應用需求的不斷增長,開發者在選擇開發框架方面也越來越有選擇的空間。 Symfony和Yii2是兩個備受歡迎的PHP框架,它們都具有強大的功能和效能,但在面對需要開發大型網路應用程式時,哪個框架更適合呢?接下來我們將對Symphony和Yii2進行比較分析,以幫助你更好地進行選擇。基本概述Symphony是一個由PHP編寫的開源Web應用框架,它是建立

如果您問「Yii是什麼?」請參閱我之前的教學:Yii框架簡介,其中回顧了Yii的優點,並概述了2014年10月發布的Yii2.0的新增功能。嗯>在這個使用Yii2程式設計系列中,我將指導讀者使用Yii2PHP框架。在今天的教學中,我將與您分享如何利用Yii的控制台功能來執行cron作業。過去,我在cron作業中使用了wget—可透過Web存取的URL來執行我的後台任務。這引發了安全性問題並存在一些效能問題。雖然我在我們的啟動系列安全性專題中討論了一些減輕風險的方法,但我曾希望過渡到控制台驅動的命令

隨著網路的快速發展,API成為了各種應用間資料交換的重要方式。因此,開發一款易於維護、高效、穩定的API框架變得越來越重要。而在選擇API框架時,Yii2和Symfony是兩個備受開發者歡迎的選擇。那麼,哪一個比較適合API開發呢?本文將對這兩個框架進行比較,並給出一些結論。一、基本介紹Yii2和Symfony都是成熟的PHP框架,都有相應的擴展,可以用來開

使用Docker容器化和部署Yii應用的步驟包括:1.創建Dockerfile,定義鏡像構建過程;2.使用DockerCompose啟動Yii應用和MySQL數據庫;3.優化鏡像大小和性能。這不僅涉及到具體的技術操作,還包括理解Dockerfile的工作原理和最佳實踐,以確保高效、可靠的部署。
