Php implements simple permission management sample code and permission management sample code

Source: Internet
Author: User

Php implements simple permission management sample code and permission management sample code

Today, we mainly implement a permission management system to set different permissions for different users, so that users with different permissions can use different functions after logon. First, let's take a look at the database.

A total of five tables, qx_user, qx_rules, and qx_juese, form a "w" relationship with the other two tables, which is also a common method of permission database, first, Set permissions first, that is, the management layer 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"> 

Chuli. php

<?phpinclude("../../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;}

Effect ,:

I can select which user to set permissions for and what role to give to, which can be one or more. Click OK to grant this permission to the database.

For example, Ma Qi has two roles, foreground and market.

Now, delete the front-end and add Finance

Let's see if the database has been added

Maqithat item has been changed, j003 and j004 are the market and financial roles.

Next, log on to an account and view its functions.

Login. php

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Logincl. php

<?phpsession_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"> 

Effect ,:

Shows that Li Si's functions are:

Check whether the database is the same:

The results are the same. This completes permission management.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.