<?php namespace Common\Controller; use Think\Controller; class CommonController extends Controller { public function _initialize() { $access = $this->checkAccess(); if($access == 0){ echo "<script>alert('Sorry, you do not currently have permission to do this...')</script>"; } } //检测session值是否存在 public function checkSession() { } //检测变量是否存在二维数组中 public function deep_in_array($value, $array) { foreach($array as $item) { if(!is_array($item)) { if ($item == $value) { return true; } else { continue; } } if(in_array($value, $item)) { return true; } else if($this->deep_in_array($value, $item)) { return true; } } return false; } //检测权限 public function checkAccess() { $controller = CONTROLLER_NAME; $action = ACTION_NAME; $access = $this->getAccess(); $con = $this->deep_in_array($controller,$access); $act = $this->deep_in_array($action,$access); if($con){ if($act){ return 1; }else{ return 0; } }else{ return 0; } } //获取权限 public function getAccess() { $sql = "select * from admin where id = 2"; $model = M(); $admin = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sql); $rid = $admin[0]["rid"]; $sqls = "select * from role where id in($rid)"; $role = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sqls); foreach($role as $k=>$v){ $jid[] = explode(",",$v['jid']); } $jid = array_unique($jid); $jid = implode(",",$jid[0]); $sqlsd = "select * from jurisdiction where id in($jid)"; $jurisdiction = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sqlsd); return $jurisdiction; } }
TP 3.2.3 权限控制源码
最新推荐文章于 2022-09-26 14:12:22 发布
本文介绍了一个基于PHP的权限控制系统实现方案。通过定义CommonController类,实现了用户权限验证的基本功能,包括初始化权限检查、会话验证、变量在二维数组中的查找以及具体的权限验证逻辑。文章详细解释了如何通过数据库查询来获取并解析用户的权限信息。

2074

被折叠的 条评论
为什么被折叠?



