PHP implementation of Rights management functions

Source: Internet
Author: User
Tags button type

Rights management system, it is mainly in order to give different users to set different permissions, so that users with different permissions to log on after the use of the same function.

First look at the database

There are a total of 5 tables, users,roles and roleswork 3 tables with another 2 tables form a "W" type of relationship, is also a more common way to access the database, first of all to do the permission set, that is, the management layer to different users set different permissions.

1. Admin page rbac.php

<!        DOCTYPE html>Please select User:<select id= "user" > <?PHPrequire_once"./dbda.class.php"; $db=NewDbda (); $sql= "SELECT * from Users"; $arr=$db->query ($sql, 0); foreach($arr  as $v){                Echo"<option value= ' {$v[0]} ' >{$v[2]} </option> "; }            ?> </select> </div> <br/> <div>Please select a role:<?PHP$sql= "SELECT * from Roles"; $arr=$db->query ($sql, 0); foreach($arr  as $v){                Echo"<input type= ' checkbox ' class= ' ck ' value= ' {$v[0]} ' >{$v[1]} "; }            ?> </div> <br/> <input type= "button" value= "Confirm" id= "btn"/> </body> <script>sel ();  // Select default Role   $ ("#user"). Change (function(){
       // When the user selects the change, select the appropriate role sel (); })
      // Click OK to save the role information $ ("#btn"). Click (function(){ varUID = $ ("#user").Val (); varCK = $ (". CK"); varrole = "";//initially empty for(i=0;i<ck.length;i++){ varv = ck.eq (i).Val (); if(Ck.eq (i). Prop ("checked")) ) {role+ = Ck.eq (i). Val () + "|"; } } $.Ajax ({type: "POST",URL: "Rbbtnchuli.php",Data: {Uid:uid,role:role},DataType: "TEXT",Success:function(data) {alert ("Modified successfully!"); } }); })
      // Select the package method for the default role functionsel () {varUID = $ ("#user").Val (); $.Ajax ({URL: "Rbchuli.php",Data: {Uid:uid},type: "POST",DataType: "TEXT",Success:function(data) {varRole = data.Split("|"); varCK = $ (". CK"); CK. Prop ("Checked",false); for(i=0;i<ck.length;i++){ varv = ck.eq (i).Val (); if(Role.indexof (v) >=0) {ck. EQ (i). Prop ("Checked",true); } } } }); } </script>

2. Do the admin processing page rbchuli.php

<? PHP $uid $_post ["UID"]; require_once "./dbda.class.php"; $db New Dbda (); $sql = "Select Rolesuid from Users_roles where usersuid= ' {$uid} '"; Echo $db->strquery ($sql, 0);

Save the processing page for role information rbbtnchuli.php

<?php$uid = $_post["UID"]; $role = $_post["Role"];//string $role = substr ($role, 0,strlen ($role)-1); $arr = Explode ("|", $ role); require_once "./dbda.class.php"; $db = new Dbda ();//Delete $sdel = "Delete from Users_roles where usersuid= ' {$uid} '"; $ Db->query ($sdel);//Add foreach ($arr as $v) {$sql = "insert into Users_roles values (0, ' {$uid} ', ' {$v} ')"; $db->query ( $sql);}

Effect

The next thing to do is log in to an account and see your own functions.

3. User Login Page rblogin.php

<!        DOCTYPE html>title{margin-left:600px; Margin-top:150px; }        .quanju{margin-left:450px; Margin-top:-180px; }        . Name,.pwd{Max-width:120px; }        .yangshi1{margin-top:200px; }    </style> <body> <formclass= "Form-horizontal" role= "form" action= "rbloginchuli.php" method= "POST" > class= "title" > User Login class= "Quanju" > <divclass= "Form-group yangshi1" > <label for= "FirstName"class= "Col-sm-2 Control-label" > User name:</label> <divclass= "Col-sm-10" > <input type= "Text"class= "Form-control name" name= "UID" placeholder= "Please enter user name" > </div> </div> &L T;divclass= "Form-group Yangshi2" > <label for= "LastName"class= "Col-sm-2 Control-label" > Password:</label> <divclass= "Col-sm-10" > <input type= "Text"class= "Form-control pwd" name= "pwd" placeholder= "Please enter password" > </div> </div> < Divclass= "Form-group" > <divclass= "Col-sm-offset-2 col-sm-10" > <divclass= "checkbox" > <label> <input type= "checkbox" >Save Password</label> <label> <input type= "checkbox" >Next Automatic login</label> </div> </div> </div> <divclass= "Form-group" > <divclass= "Col-sm-offset-2 col-sm-10" > <button type= "Submit"class= "Btn btn-warning" value= "Login" onclick= "return Login ()" >Login</button> </div> </div> </div> </fo Rm> </body>

4. The processing page of the login page rbloginchuli.php

<?PHPSession_Start();$uid=$_post["UID"];$pwd=$_post["PWD"];require_once"./dbda.class.php";$db=NewDbda ();$sql= "Select pwd from users where uid= ' {$uid}‘";$mm=$db->strquery ($sql, 0);if(!Empty($pwd) &&$pwd==$mm){    $_session["UID"] =$uid; Header("location:RBmain.php");}Else{    Echo"<script>alert (' username or password is incorrect! ') </script> ";}

5. Finally do the user's homepage face rbmain.php

<!            DOCTYPE html>PHPSession_Start(); if(Empty($_session["UID"])){            Header("location:RBlogin.php"); Exit; }        $uid=$_session["UID"]; require_once"./dbda.class.php"; $db=NewDbda (); //Sub-query        $sql= "SELECT * from Roleswork where code in (SELECT * from Roles_roleswork where rolesuid in (SELECT * from Users_roles where Usersuid= ' {$uid}‘))"; $arr=$db->query ($sql, 0); foreach($arr  as $v){            Echo"<div class= ' menu ' >{$v[1]} </div> "; }                ?> </body>

Effect

PHP implementation of Rights management functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.