首先權限管理肯定是需要登陸的,這里就簡單的寫一個登陸頁面。
簡單的登陸頁面login.php
<h1>登錄頁面</h1> <form action="logincl.php" method="post"> <input type="text" name="uid" /> <input type="password" name="pwd" /> <input type="submit" value="登錄" /> </form>
登陸處理頁面logincl.php
<?php session_start(); include("../FENGZHUANG/DBDA.class.php"); $db = new DBDA(); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; $sql="select pwd from users where uid='{$uid}'"; $mm = $db->StrQuery($sql); if($mm==$pwd && !empty($pwd)) { $_SESSION["uid"]=$uid; header("location:main.php"); }
登陸完成后進入主頁面main.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <h1>主頁面</h1> <?php session_start(); include("../FENGZHUANG/DBDA.class.php"); $db = new DBDA(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } //登錄者用戶名 $uid = $_SESSION["uid"]; //根據用戶名查角色 $sjs = "select jueseid from userinjuese where userid='{$uid}'"; $ajs = $db->Query($sjs); //定義一個存放功能代號的數組 $arr = array(); //根據角色代號查功能代號 foreach($ajs as $vjs) { $jsid = $vjs[0]; //角色代號 $sgn = "select ruleid from juesewithrules where jueseid='{$jsid}'"; $strgn = $db->StrQuery($sgn); $agn = explode("|",$strgn); foreach($agn as $vgn) { array_push($arr,$vgn); } } //去重,顯示 $arr = array_unique($arr); foreach($arr as $v) { $sql = "select * from rules where code='{$v}'"; $attr = $db->Query($sql); $attr[0][0]; $attr[0][1]; echo "<div code='{$attr[0][0]}'>{$attr[0][1]}</div>"; } ?> </body> </html>
管理權限界面guanli.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <script src="../FENGZHUANG/jquery-3.1.1.min.js"></script> </head> <body> <h1>用戶與角色管理</h1> <div> 請選擇用戶: <select id="user"> <?php include("../FENGZHUANG/DBDA.class.php"); $db = new DBDA(); $sql = "select * from users"; $arr = $db->Query($sql); foreach($arr as $v) { echo "<option value='{$v[0]}'>{$v[2]}</option>"; } ?> </select> </div> <br /> <div> 請選擇角色: <?php $sjs = "select * from juese"; $ajs = $db->Query($sjs); foreach($ajs as $v) { echo "<input type='checkbox' value='{$v[0]}' class='ck' />{$v[1]} "; } ?> </div> <br /> <input type="button" value="確定" id="btn" /> </body> <script type="text/javascript"> $(document).ready(function(e) { //選中默認角色 Xuan(); //當用戶選中變化的時候,去選中相應角色 $("#user").change(function(){ Xuan(); }) //點擊確定保存角色信息 $("#btn").click(function(){ var uid = $("#user").val(); var juese = ""; var ck = $(".ck"); for(var i=0;i<ck.length;i++) { if(ck.eq(i).prop("checked")) { juese += ck.eq(i).val()+"|"; } } juese = juese.substr(0,juese.length-1); $.ajax({ url:"chuli.php", data:{uid:uid,juese:juese,type:1}, type:"POST", dataType:"TEXT", success: function(data){ alert("保存成功!"); } }); }) }); //選中默認角色 function Xuan() { var uid = $("#user").val(); $.ajax({ url:"chuli.php", data:{uid:uid,type:0}, type:"POST", dataType:"TEXT", success: function(data){ var juese = data.trim().split("|"); var ck = $(".ck"); ck.prop("checked",false); for(var i=0;i<ck.length;i++) { if(juese.indexOf(ck.eq(i).val())>=0) { ck.eq(i).prop("checked",true); } } } }); } </script> </html>
處理界面chuli.php
<?php include("../FENGZHUANG/DBDA.class.php"); $db = new DBDA(); $type = $_POST["type"]; switch($type) { case 0: $uid = $_POST["uid"]; $sql = "select jueseid from userinjuese where userid='{$uid}'"; echo $db->StrQuery($sql); break; case 1: $uid = $_POST["uid"]; $juese = $_POST["juese"]; $sdel = "delete from userinjuese where userid='{$uid}'";//保存權限最簡單的辦法是先把其刪除,再重新添加。 $db->Query($sdel,0); $arr = explode("|",$juese); foreach($arr as $v) { echo $v; $sql = "insert into userinjuese values('','{$uid}','{$v}')"; $db->Query($sql,0); } echo "OK"; break; }
數據庫截圖: