2016/04/26 权限 数据库mydb2 五个表 分别是 1,用户 2,角色 3,权限 4,用户对应的角色 5,角色对应的权限
权限: 1,后台分配角色 角色对应权限 2,各用户通过登录页面登录 查看到各自的权限
五个页面 加引入一个jquery-1.11.2.min.js
1,guanli.php (后台管理页面,分配角色)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用户与角色权限管理</title> 6 <script type="text/javascript" src="jquery-1.11.2.min.js"></script> 7 </head> 8 <body> 9 <h1>权限管理</h1> 10 <div>请选择用户: 11 <select id="user"> 12 <?php 13 include("../DBDA.php"); 14 $db=new DBDA(); 15 $sqlu="select * from Users"; 16 17 18 $str=$db->StrQuery($sqlu,1,"mydb2"); 19 20 $hangu=explode("|", $str); 21 for ($i=0; $i <count($hangu) ; $i++) { 22 list($UserName,$Password,$Name)=explode("^",$hangu[$i]); 23 echo "<option value='{$UserName}'>{$Name}</option>"; 24 } 25 26 27 ?> 28 </select><br><br> 29 <div>请选择角色:</div><br> 30 <div> 31 32 <?php 33 $sqlj="select * from JueSe"; 34 $strj=$db->StrQuery($sqlj,1,"mydb2"); 35 $hangj=explode("|", $strj); 36 for ($i=0; $i <count($hangj) ; $i++) { 37 list($code,$name)=explode("^", $hangj[$i]); 38 echo "<input type='checkbox' value='{$code}' class='juese'/>{$name} "; 39 40 } 41 42 ?> 43 </div><br> 44 <div><input type="button" id="sure" value="确定"></input></div> 45 46 47 </div> 48 </body> 49 <script type="text/javascript"> 50 $(document).ready(function(e){ 51 Xianshi($("#user").val()); 52 //选中用户显示其角色 53 $("#user").change(function(){ 54 var uid=$(this).val(); 55 Xianshi(uid); 56 }); 57 58 function Xianshi(uid){ 59 var ckall=$(".juese"); 60 for (var i = 0; i < ckall.length; i++) { 61 ckall.removeAttr("checked"); 62 } 63 $.ajax({ 64 url:"chulijuese.php", 65 data:{uid:uid}, 66 type:'POST', 67 dataType:"TEXT", 68 success:function(data){ 69 if (!data=="") { 70 var hang=data.split("|"); 71 for (var i = 0; i < hang.length; i++) { 72 var lie=hang[i].split("^"); 73 var ck=$(".juese"); 74 75 76 for (var j = 0; j < ck.length; j++) { 77 if (ck.eq(j).val()==lie[2]) { 78 ck.eq(j).prop("checked","true"); 79 } 80 } 81 } 82 } 83 84 85 } 86 }); 87 } 88 89 90 //添加按钮 91 $("#sure").click(function(){ 92 var uid=$("#user").val(); 93 var ck=$(".juese"); 94 var str=""; 95 for (var j = 0; j < ck.length; j++) { 96 if (ck.eq(j)[0].checked==true) { 97 str=str+ck.eq(j).val()+"|"; 98 } 99 } 100 //alert(str.substr(0,str.length-1)); 101 str=str.substr(0,str.length-1); 102 $.ajax({ 103 url:"chuliadd1.php", 104 data:{uid:uid,str:str}, 105 type:"POST", 106 dataType:"TEXT", 107 success:function(data){ 108 if (data=="OK") { 109 alert("操作成功!"); 110 } 111 } 112 113 }); 114 }) 115 }); 116 </script> 117 </html>
2,chulijuese.php(判断数据库中的数据然后确定checkbox是否可以被选中 角色前面的框)
1 <?php 2 include("../DBDA.php"); 3 $uid=$_POST["uid"]; 4 $db=new DBDA(); 5 $sql="select * from UserInJueSe where UserId='{$uid}'"; 6 $str=$db->StrQuery($sql,1,"mydb2"); 7 8 echo $str; 9 10 11 ?>
3,chuliadd1.php (从guanli页面传过值来,先清掉原有的角色选中,然后根据传来的值重新选中)
1 <?php 2 include("../DBDA.php"); 3 $uid=$_POST["uid"]; 4 $str=$_POST["str"]; 5 6 $db=new DBDA(); 7 8 $sqld="delete from UserInJuese where UserId='{$uid}'"; 9 10 $fh=$db->StrQuery($sqld,0,"mydb2"); 11 12 if ($fh="OK") { 13 $juese=explode("|", $str); 14 for ($i=0; $i <count($juese) ; $i++) { 15 $sql="insert into UserInJueSe values ('','".$uid."','".$juese[$i]."')"; 16 $db->StrQuery($sql,0,"mydb2"); 17 } 18 echo "OK"; 19 } 20 ?>
4,denglu.php (数据库中的人员根据姓名和密码登录)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录页面</title> 6 </head> 7 <body> 8 <h1>登录页面</h1> 9 <form action="main.php" method="post"> 10 用户名:<input type="text" name="uid" id="uid"></input><br><br> 11 密 码:<input type="text" name="pwd" id="pwd"></input><br> 12 <input type="submit" value="登录"></input> 13 </form> 14 </body> 15 </html>
5,main.php(显示各个角色具有的各自权限)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>主页面</title> 6 </head> 7 <body> 8 div 9 <?php 10 session_start(); 11 include("../DBDA.php"); 12 $db=new DBDA(); 13 $uid=$_POST["uid"]; 14 $pwd=$_POST["pwd"]; 15 16 $sql="select count(*) from Users where Username='".$uid."' and PassWord ='".$pwd."'"; 17 $str=$db->StrQuery($sql,1,"mydb2"); 18 if ($str==1) { 19 $_SESSION["uid"]=$uid; 20 } 21 else{ 22 header("Location:denglu.php"); 23 } 24 ?> 25 <div style="width:1000px; height: 35px; background-color:blue; color: white; font-size:25px;"> 26 <div style="width: 150px; height: 35px; vertical-align: middle; line-height: 35px;text-align: center; float: left;">权限管理</div> 27 <?php 28 $sqlj="select * from UserInJueSe where UserId='{$uid}'"; 29 $strj=$db->StrQuery($sqlj,1,"mydb2"); 30 $hangj=explode("|", $strj); 31 32 $xianshi=array(); 33 34 for ($i=0; $i <count($hangj) ; $i++) { 35 $liej=explode("^",$hangj[$i]); 36 37 $sqlr="select RuleId from JueSeWithRules where JueSeId='".$liej[2]."'"; 38 $strr=$db->StrQuery($sqlr,1,"mydb2"); 39 $hangr=explode("|", $strr); 40 41 $xianshi=array_merge($xianshi,$hangr); 42 43 } 44 $xianshi=array_unique($xianshi); 45 46 for ($i=0; $i < count($xianshi); $i++) { 47 $xianshi[$i]; 48 $sqlname="select * from Rules where Code='".$xianshi[$i]."'"; 49 $strname=$db->StrQuery($sqlname,1,"mydb2"); 50 list($code,$name)=explode("^",$strname); 51 echo "<div style='width: 150px; height: 35px; vertical-align: middle; line-height: 35px;text-align: center; float:left;'>{$name}</div>"; 52 } 53 54 55 ?> 56 </div> 57 </body> 58 </html>
lisi的权限:
zhangsan的权限: