example_php权限管理

Login.html

<!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>
<form action="LoginChuLi.php" method="post">

<div>用户名:<input type="text" name="uid"/></div>

<br />

<div>密码   :<input type="text" name="pwd"/></div>


<input type="submit" value="登陆" />
</form>

</body>
</html>
View Code

LoginChuLi.php

<?php 
    session_start();
    
    $uid = $_POST["uid"];
    
    $pwd = $_POST["pwd"];
    
    include("../../Public/Connect.class.php");
    
    $con = new Connect();
    
    $sql = "select count(*) from Users where UserName = '{$uid}' and Password = '{$pwd}'";
    
    $str = $con->Query_string($sql);
    
    if($str==1)
{
    $_SESSION["uid"] = $uid;
    
    header("location:Main.php");
}
    else
{
    header("location:Login.html");    
}

?>
View Code

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>
<style type="text/css">

*{border:0; padding:0; margin:0}

.top {800px; height:50px; background:#E9E9E9; color:#00F; margin:0 auto;}

.menu { text-align:center; 100px; line-height:50px; float:left;}

.menu a{ text-decoration:none;}

</style>
</head>

<body>
<h1>主页面</h1>
<div class="top">
<?php
    
              session_start();
              
              $uid = "";

              if(!empty($_SESSION["uid"]))
            {
                $uid = $_SESSION["uid"];
            }    
              else
            {
                header("location:Login.html");
            }
            
            include("../../Public/Connect.class.php");
            
            $con = new Connect();
            
            //根据用户名查角色代号
            $sql = "select JueSeId from userinjuese where UserId = '{$uid}'";
            
            //可能是一个也可能是多个
            $arr = $con->Query_array($sql);
        
            //存放功能代号的数组
            $att = array();

            for($i=0;$i<count($arr);$i++)
        {
            //变量接收角色代号
            $js = $arr[$i][0];
            
            if($js == "j001")
         {
            echo "<div class = 'menu'><a href = 'QuanXian.php'>权限管理</a></div>";    
         }
            
            //根据角色代号查功能
            $sqlr = "select RuleId from juesewithrules where JueSeId = '{$js}'";
            
            $attr = $con->Query_string($sqlr);

            //拆分字符串成二维数组
            $attr = explode("|",$attr);
            
            //合并数组
            $att = array_merge($att,$attr);
        }
            
            //去掉重复的功能代号
            $att = array_unique($att);
            

            //根据功能代号查功能名称
            for($i=0;$i<count($att);$i++)
        {
            $sql = "select Name from rules where Code = '{$att[$i]}'";
            
            $name = $con->Query_string($sql);
            
            echo "<div class = 'menu'>{$name}</div>";
        }
?>
</div>
</body>
</html>
View Code

QuanXian.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" />
<script src="../../Public/jquery/jquery-2.2.3.min.js"></script>
<title>无标题文档</title>
</head>

<body>
<h1>权限管理</h1>


<?php
        
        include("../../Public/Connect.class.php");
        
        $con = new Connect();
        
        $sqlu = "select * from users";
        
        $arru = $con->Query_array($sqlu);
        
        $sqlj = "select * from juese";
        
        $arrj = $con->Query_array($sqlj);
?>

<div>请选择用户:<select id="user">

<?php
        
            for($i=0;$i<count($arru);$i++)
        {
            echo "<option value = '{$arru[$i][0]}'>{$arru[$i][2]}</option>";
        }
        

?>

</select>
</div><br />

<div>请选择角色:</div>
<div>
<?php 
            
            for($i=0;$i<count($arrj);$i++)
        {
            echo "<input type='checkbox' value='{$arrj[$i][0]}' class='juese'/>{$arrj[$i][1]}&nbsp;";            
        }

?>
</div><br />
<div><input type="button"  value="确定" id="sure"/></div>
<script type="text/javascript">

$(document).ready(function(e) {
        
        
        
            var ckall = $(".juese");     //用变量接收所有复选框以备用
            
            
            ShowJueSe();        //调用函数,显示默认的第一个人的角色
            
    
            $("#user").change(function(e) {
                
                ShowJueSe();
            });
            
            
            $("#sure").click(function(e) {
                
                var uid = $("#user").val();    
                
                var juese = "";                    //定义一个空字符串备用
                
                    for(var i=0;i<ckall.length;i++)  //遍历复选框,取得角色代号
                {
                        if(ckall.eq(i)[0].checked)
                    {
                        juese += ckall.eq(i).val()+"|";
                    }
                }
                
                juese = juese.substr(0,juese.length-1);
                
                $.ajax({
                    
                    url:"XiuGai.php",
                    data:{uid:uid,juese:juese},
                    dataType:"TEXT",
                    type:"POST",
                    success: function(data)
                {
                    
                            alert(data);            
                    
                }
                      });
                    
            });
            
            
    
                function ShowJueSe()        //封装成函数,以备调用
            {
                var uid = $("#user").val();
                
                $.ajax({
                    
                    url:"Jschuli.php",
                    data:{uid:uid},
                    dataType:"TEXT",
                    type:"POST",
                    success: function(data)
                  {
                    $(":checkbox").removeAttr("checked");    //清空所有复选框
                    
                        if(data.trim() != "")
                    {
                        var hang = data.split("|");
                    
                            for(var i=0;i<hang.length;i++)
                        {
                              var lie = hang[i].split("^");
                              
                              //var ckall = $(".juese"); 
                            
                             for(var j=0;j<ckall.length;j++)    //遍历复选框,显示原有职位为选中状态
                           { 
                                if(ckall.eq(j).val()== lie[2])
                              {
                                 ckall.eq(j).prop("checked",true);
                              }
                           }
                           
                        }  
                        
                    }
                    
                  }
                    
                }); 
                        
            }
            

});

</script>
</body>
</html>
View Code

XiuGai.php

<?php
          
              $uid = $_POST["uid"];
              
              $juese = $_POST["juese"];
              
              include("../../Public/Connect.class.php"); 
              
              $con = new Connect();
              
              $sql = "delete from userinjuese where UserId = '{$uid}'";  //先清空角色,再添加
              
              $con->Query_string($sql,0);
              
              
              $juese = explode("|",$juese);
              
              $isOK = true;
          
              for($i=0;$i<count($juese);$i++)
          {
              $sql = "insert into userinjuese values('','{$uid}','{$juese[$i]}')";
      
              $isOK = $isOK && $con->Query_array($sql,0);
          }
          
              if($isOK)
          {
              echo "修改成功!";
          }
              else
          {
              echo "修改失败!";
          }
      
?>
View Code

JsChuli.php

<?php 

    $uid = $_POST["uid"];

    include("../../Public/Connect.class.php");
    
    
    $con = new Connect();
    
    $sql = "select * from userinjuese where UserId = '{$uid}'";
    
    $str = $con->Query_string($sql);
    
    echo $str;

?>
View Code
原文地址:https://www.cnblogs.com/sihuiming/p/5433394.html