Home > Article > Backend Development > PHP combined with mysql to achieve permission management
In our actual PHP development, it is very important to manage user permissions. To set different permissions for different users, we will inevitably use the mysql database. In this article, we will explain how PHP works. Combining mysql to implement permission management
Today we mainly implement a permission management system. It is mainly to set different permissions for different users, so that users with different permissions can use different functions after logging in. First look at the database
#There are a total of 5 tables. The 3 tables qx_user, qx_rules and qx_juese form a "w"-shaped relationship with the other 2 tables. It is also a relatively common permission database method. First, set the permissions. Defined, that is, the management sets different permissions for different users. 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="../../dist/js/jquery-1.11.2.min.js"></script> </head> <body> <h1>用户与角色管理</h1> <p> 请选择用户: <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> </p> <br /> <p> 请选择角色: <?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]} "; } ?> </p> <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 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; }
The effect achieved is as shown in the figure:
I can choose which user to set permissions and what role to give him. It can be one or multiple. Click OK to grant the permission in the database.
For example: Ma Qi itself has two roles: front desk and market
Now, delete the front desk and add finance
Then let’s see if the database has been added
The item Ma Qi has been changed, j003 and j004 are the marketing and financial roles.
The next thing to do is to log in to an account and check your functions
login.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="logincl.php" method="post"> <input type="text" name="uid" /> <input type="password" name="pwd" /> <input type="submit" value="登录" /> </form> </body> </html>
logincl.php
<?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
<!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 "<p code='{$attr[0][0]}'>{$attr[0][1]}</p>"; } ?> </body> </html>
The completed effect is as shown in the picture:
##It shows that Li Si’s function is:
Check if the database is the same:
The results are found to be the same. This completes the permission management. You can take a good look and practice, it will be of great help in our future development!
The above is the detailed content of PHP combined with mysql to achieve permission management. For more information, please follow other related articles on the PHP Chinese website!