流程:
在权限管理页面,默认显示用户的角色,更改复选框的按钮内容,可以改变角色,点击确定提交至数据库
在登陆之后,只显示该用户的角色所对应的权限
数据库:
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="../jquery-1.11.2.min.js"></script> </head> <body> <h1>权限管理</h1> <div>请选择用户: <select id="user"> <?php include("../mydbda.php"); $db = new mydbda(); $sqlu = "select * from Users"; $str = $db->Select($sqlu,"CX","mydb"); $hangu = explode("|",$str); for($i=0;$i<count($hangu);$i++) { list($uid,$pwd,$name) = explode("^",$hangu[$i]); echo "<option value='{$uid}'>{$name}</option>"; } ?> </select></div><br /> <div>请选择角色:</div><br /> <div> <?php $sqlj = "select * from JueSe"; $strj = $db->Select($sqlj,"CX","mydb"); $hangj = explode("|",$strj); for($i=0;$i<count($hangj);$i++) { list($code,$name) = explode("^",$hangj[$i]); echo "<input type='checkbox' class='juese' value='{$code}' />{$name} "; } ?> </div><br /> <div><input type="button" id="sure" value="确定"/></div> </body> <script type="text/javascript"> $(document).ready(function(e) { Xianshi($("#user").val()); //选中用户,显示其角色 $("#user").change(function(){ var uid =$(this).val(); Xianshi(uid); }) function Xianshi(uid) { var ckall = $(".juese"); for(var j=0;j<ckall.length;j++) { ckall.removeAttr("checked"); //ckall.attr("checked","false"); //document.getElementById().setAttribute( } $.ajax({ url:"chulijuese.php", data:{uid:uid}, type:"POST", dataType:"TEXT", success: function(data){ if(!data == "") { var hang = data.split("|"); for(var i=0;i<hang.length;i++) { var lie = hang[i].split("^"); var ck = $(".juese"); for(var j=0;j<ck.length;j++) { if(ck.eq(j).val() == lie[2]) { ck.eq(j).prop("checked","true"); } } } } } }); } //添加按钮 $("#sure").click(function(){ var uid = $("#user").val(); var ck = $(".juese"); var str = ""; for(var j=0;j<ck.length;j++) { if(ck.eq(j)[0].checked == true) { str = str + ck.eq(j).val()+"|"; } } str = str.substr(0,str.length-1); $.ajax({ url:"chuliadd.php", data:{uid:uid,str:str}, type:"POST", dataType:"TEXT", success: function(data){ if(data == "OK") { alert("操作成功!"); } } }); }) }); </script> </html>
chuliadd.php
<?php include("../mydbda.php"); $db = new mydbda(); $uid = $_POST["uid"]; $str = $_POST["str"]; $sqld = "delete from UserInJueSe where UserId = '{$uid}'"; $jg = $db->Select($sqld,"QT","mydb"); if($jg == "OK") { $juese = explode("|",$str); for($i=0;$i<count($juese);$i++) { $sql = "insert into UserInJueSe values('','".$uid."','".$juese[$i]."')"; $db->Select($sql,"QT","mydb"); } echo "OK"; } ?>
chulijuese.php
<?php include("../mydbda.php"); $uid = $_POST["uid"]; $sql = "select * from UserInJueSe where UserId = '{$uid}'"; $db = new mydbda(); $str = $db->Select($sql,"CX","mydb"); echo $str; ?>
denglu.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>
<form action="main.php" method="post">
<div>用户名:<input type="text" name="uid"/></div>
<div>密码:<input type="text" name="pwd"/></div>
<input type="submit" value="登录"/>
</form>
</body>
</html>
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 include("../mydbda.php"); $db = new mydbda(); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; $sql = "select count(*) from Users where Uid ='".$uid."' and Pwd = '".$pwd."'"; $str = $db->Select($sql,"CX","mydb"); if($str == 1) { } else { header("Location:denglu.php"); } ?> <div style="width:100%; height:35px; background-color:#60F; color:white; font-size:20px;"> <div style="width:150px; height:35px; vertical-align:middle; line-height:35px; float:left; text-align:center;">权限管理</div> <?php $sqlj = "select * from UserInJueSe where UserId = '{$uid}'"; $strj = $db->Select($sqlj,"CX","mydb"); $hangj = explode("|",$strj); $xianshi = array(); for($i=0; $i<count($hangj);$i++) { $liej = explode("^",$hangj[$i]); $sqlr = "select RuleId from JueSeWithRules where JueSeId = '".$liej[2]."'"; $strr = $db->Select($sqlr,"CX","mydb"); $hangr = explode("|",$strr); $xianshi = array_merge($xianshi,$hangr);//合并数组 } $xianshi = array_unique($xianshi); //去重 for($i=0; $i<count($xianshi);$i++) { $sqlname = "select * from Rules where Code = '".$xianshi[$i]."'"; $strname = $db->Select($sqlname,"CX","mydb"); list($code,$name) = explode("^",$strname); echo "<div style='width:150px; height:35px; vertical-align:middle; line-height:35px; float:left; text-align:center;'>{$name}</div>"; } ?> </div> </body> </html>