php后台如何避免用户直接进入方法
生活随笔
收集整理的這篇文章主要介紹了
php后台如何避免用户直接进入方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1)創(chuàng)建BaseController控制器繼承Controller(后臺的一切操作要繼承BaseController):
在BaseController里面添加:
public function checkLogin() {if (Yii::app()->authority->isLogin() == Yii::app()->authority->getStatus('NOTLOGIN')) {$url = $this->createUrl('user/login');if (Yii::app()->request->isPostRequest && Yii::app()->request->isAjaxRequest) {echo json_encode(array('code' => -101, 'message' => '用戶未登錄。', 'callback' => 'window.location="' . $url . '";'));} else if (Yii::app()->request->isAjaxRequest) {echo '<script language="javascript">window.location="' . $url . '";</script>';} else {$this->redirect($url);}exit;}return true;}在components目錄下創(chuàng)建Authority.php文件:
<?php/*** 權(quán)限檢查組件*/ class Authority extends CComponent {private $NOTLOGIN = -1;private $FAILED = -2;private $PASS = 1;public function init() {}/*** 檢查是否登陸* @return boolean */function isLogin() {return isset(Yii::app()->session['user']) ? $this->PASS : $this->NOTLOGIN;}/*** 獲取狀態(tài)值* @param string $name* @return int */public function getStatus($name){return $this->$name;} }
轉(zhuǎn)載于:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3370267.html
總結(jié)
以上是生活随笔為你收集整理的php后台如何避免用户直接进入方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu 上已经安装libxml2还
- 下一篇: Android开发记录(转)