今天主要来实现一个权限管理系统,它主要是为了给不同的用户设定不同的权限,从而实现不同权限的用户登录之后使用的功能不一样,首先先看下数据库
总共有5张表,qx_user,qx_rules和qx_juese 3张表与另外2张表形成"w"型的关系,也是比较常见的一种权限数据库的方式,首先先做权限的设定,也就是管理层给不同用户设定不同权限。
guanli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<!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= "../../dist/js/jquery-1.11.2.min.js" ></script>
</head>
<body>
<h1>用户与角色管理</h1>
<div>
请选择用户:
<select id= "user" >
<?php
include ( "../../fengzhuang/dbda.class.php" );
$db = new dbda();
$sql = "select * from qx_user" ;
$arr = $db ->query( $sql );
foreach ( $arr as $v )
{
echo "<option value='{$v[0]}'>{$v[2]}</option>" ;
}
?>
</select>
</div>
<br />
<div>
请选择角色:
<?php
$sjs = "select * from qx_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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php
include ( "../../fengzhuang/dbda.class.php" );
$db = new dbda();
$type = $_post [ "type" ];
switch ( $type )
{
case 0:
$uid = $_post [ "uid" ];
$sql = "select jueseid from qx_uij where useid='{$uid}'" ;
echo $db ->strquery( $sql );
break ;
case 1:
$uid = $_post [ "uid" ];
$juese = $_post [ "juese" ];
$sdel = "delete from qx_uij where useid='{$uid}'" ;
$db ->query( $sdel ,0);
$arr = explode ( "|" , $juese );
foreach ( $arr as $v )
{
echo $v ;
$sql = "insert into qx_uij values('','{$uid}','{$v}')" ;
$db ->query( $sql ,0);
}
echo "ok" ;
break ;
}
|
实现的效果,如图:
我可以选择给哪个用户设置权限,给他一个什么角色,可以是一个,也可以多个,点击确定就在数据库中赋予了该权限。
例如:马七本身有前台和市场2个角色
现在,删除前台,增加财务
那我们看看数据库添加了没有
马七那项已经改了,j003和j004就是市场和财务角色。
接下来做的是登录某个账号,查看自己的职能
login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!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= "logincl.php" method= "post" >
<input type= "text" name= "uid" />
<input type= "password" name= "pwd" />
<input type= "submit" value= "登录" />
</form>
</body>
</html>
|
logincl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
session_start();
include ( "../../fengzhuang/dbda.class.php" );
$db = new dbda();
$uid = $_post [ "uid" ];
$pwd = $_post [ "pwd" ];
$sql = "select pwd from qx_user where uid='{$uid}'" ;
$mm = $db ->strquery( $sql );
if ( $mm == $pwd && ! empty ( $pwd ))
{
$_session [ "uid" ]= $uid ;
header( "location:main.php" );
}
|
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<!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 qx_uij where useid='{$uid}'" ;
$ajs = $db ->query( $sjs );
//定义一个存放功能代号的数组
$arr = array ();
//根据角色代号查功能代号
foreach ( $ajs as $vjs )
{
$jsid = $vjs [0]; //角色代号
$sgn = "select ruleid from qx_jwr 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 qx_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>
|
完成的效果,如图:
显示李四的职能是:
看看数据库的是不是一样的:
发现结果是一样的。这样权限管理就做完了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/shenzikun1314/p/6604867.html