Here's a small piece to bring you a PHP personnel Rights Management (RBAC) instance (recommended). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
php-Personnel Rights Management (RBAC)
Rights management can want to do VIP functions, ordinary users and VIP users of the function is not the same, roughly will use five tables : User tables, role tables, function tables, as well as their interrelated tables: User and Role table, role and function table
The five tables I used are as follows:
First, the Administrator page is written
1. Use the drop-down list to display the user name
<p> <select id= "user" > <?php require ". /dbda.class.php "; $db = new Dbda (); $sql = "SELECT * from users"; $arr = $db->query ($sql, 1); foreach ($arr as $v) { echo "<option value= ' {$v [0]} ' >{$v [2]}</option>"; } ? > </select></p>
2. Because a new object has been created above, start writing directly from the SQL statement when the role name is displayed
<p> Please select the role: <?php $sql = "SELECT * from Juese"; $arr = $db->query ($sql, 1); foreach ($arr as $v) { echo "<input type= ' checkbox ' class= ' ck ' value= ' {$v [0]} '/>{$v [1]} '; } ? ></p><br/>
3. To modify the permissions plus a Confirm Save button
<input type= "button" value= "Save" id= "Baocun"/>
4. In this way, consider how to make the user's role in the database display, that is to use the drop-down list and the value of the check box
You can write it into the method, and then call this method
function Xuan () { var uid = $ ("#user"). Val (); $.ajax ({ URL: "chuli.php", data:{uid:uid}, type: "POST", dataType: "TEXT", success:function ( Data) { var js = Data.trim (). Split ("|"); var ck = $ (". CK"); Ck.prop ("Checked", false); for (Var i=0;i<ck.length;i++) { var v = ck.eq (i). Val (); if (Js.indexof (v) >=0) { ck.eq (i). Prop ("checked", True);}}} )}
5. Processing pages for each value
<?phprequire ". /dbda.class.php "; $db = new Dbda (); $uid = $_post[" UID "]; $sql =" Select Jueseid from Userinjuese where userid= ' {$uid} ' "; echo $db->strquery ($sql);
The effect is as follows:
6. The last is to save the modified value, you can directly delete in the re-write method to make the selection of values; Add a click event to the Save button
Xuan (); $ ("#user"). Change (function () { Xuan (); }) $ ("#baocun"). Click (function () { var uid = $ ("#user"). Val (); var str = ""; var ck = $ (". CK"); for (Var i=0;i<ck.length;i++) { if (Ck.eq (i). Prop ("checked")) { str = str + CK.EQ (i). Val () + ","; } } str = STR.SUBSTR (0,str.length-1); $.ajax ({ URL: "add.php", data:{uid:uid,js:str}, type: "POST", dataType: "TEXT", success: function (data) { alert ("Saved successfully! "); } }) })
7. Save the processing page
<?phprequire ". /dbda.class.php "; $db = new Dbda (); $uid = $_post[" UID "]; $js = $_post[" JS "]; Empty the original role $sql = "Delete from Userinjuese where userid= ' {$uid} '"; $db->query ($sql); Add the selected role $ajs = Explode (",", $js); foreach ($ajs as $v) { $sql = "INSERT into Userinjuese values (', ' {$uid} ', ' {$v} ')"; $db->query ($sql);}
The effect is as follows:
The following code is used for copy, Note that Ajax needs to reference jquery
1.guanli.php
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
2.chuli.php
<?phprequire ". /dbda.class.php "; $db = new Dbda (); $uid = $_post[" UID "]; $sql =" Select Jueseid from Userinjuese where userid= ' {$uid} ' "; echo $db->strquery ($sql);
3. Save the processing page add.php
<?phprequire ". /dbda.class.php "; $db = new Dbda (); $uid = $_post[" UID "]; $js = $_post[" JS "]; Empty the original role $sql = "Delete from Userinjuese where userid= ' {$uid} '"; $db->query ($sql); Add the selected role $ajs = Explode (",", $js); foreach ($ajs as $v) { $sql = "INSERT into Userinjuese values (', ' {$uid} ', ' {$v} ')"; $db->query ($sql);}
Two. After completing the admin page, here is the login page
1. Login to Basic page login.php
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
2. Login-Processed page dlchuli.php
<?phpsession_start (); Require ". /dbda.class.php "; $db = new Dbda (); $uid = $_post[" UID "]; $pwd = $_post[" pwd "]; $sql =" Select pwd from users where uid= ' {$uid } ' "; $mm = $db->strquery ($sql); if ($mm = = $pwd &&!empty ($pwd)) { $_session[" uid "] = $uid; Header ("location:main.php");} else{ echo "The username or password you entered is incorrect! ";}
3. Main Page main.php
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Select Login Zhang San to display his permissions, the effect is as follows: