Php personnel permission management (RBAC) instance (recommended), permission management rbac
Php-RBAC)
Permission management can be used for vip functions. The functions of common users and vip users are different. five tables are used.: User table, role table, and function table, as well as the tables that are associated with each other: user and role table, role and function table
The five tables I used are as follows:
1. The Administrator page is first written.
1. Use the drop-down list to display the user name
<div> <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></div>
2. Because a new object has been created above, you can directly write the role name from the SQL statement when displaying it.
<Div> select a 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]}" ;}?> </Div> <br/>
3. Add a confirm button to modify the permission
<Input type = "button" value = "save" id = "baocun"/>
4. In this way, we need to consider how to display the user's role in the database, that is, to use the drop-down list and check box values.
You can write it into the method and 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 page of each item 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 step is to save the modified value. You can directly use the delete all method to select the value for re-writing. Add an 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. Saved processing page
<? Phprequire ".. /DBDA. class. php "; $ db = new DBDA (); $ uid = $ _ POST [" uid "]; $ js =$ _ POST [" js "]; // clear 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: AJAX must 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. add. php
<? Phprequire ".. /DBDA. class. php "; $ db = new DBDA (); $ uid = $ _ POST [" uid "]; $ js =$ _ POST [" js "]; // clear 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 );}
2. After the Administrator page is completed, the following is the logon page.
1. log on to the 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. log on to the page for processing 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 ");} the username or password entered by else {echo" is incorrect! ";}
3. main. php on the home page
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Select login to Michael Jacob to display his permissions. The effect is as follows:
The above php personnel permission management (RBAC) instance (recommended) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for helping customers.