Php member Permissions

Source: Internet
Author: User

<? Php
Echo $ uu = @ array_sum (@ $ _ POST ['gr ']);
?>
<Form action = "" method = "POST">
ADD <input type = "checkbox" name = "gr []" value = 1 <? Php echo $ uu & 1? "Checked": null;?>
UPD <input type = "checkbox" name = "gr []" value = 2 <? Php echo $ uu & 2? "Checked": null;?>
LIS <input type = "checkbox" name = "gr []" value = 4 <? Php echo $ uu & 4? "Checked": null;?>
DEL <input type = "checkbox" name = "gr []" value = 8 <? Php echo $ uu & 8? "Checked": null;?>
<Input type = "submit" value = "submit"/>
</Form>


<? Php
Mysql_connect ("localhost", "root ","");
Mysql_select_db ("db99 ");
Mysql_query ("set names 'utf8 '");
Define ('add', 1 );
Define ('upd', 2 );
Define ('Lis ', 4 );
Define ('del ', 8 );
?>


<? Php
Include_once ('config. php ');
$ SQL = "select * from 'user _ admin' as a, 'user _ group'
As B where a. 'gro' = B. 'sid 'and a. 'uname' = ''";
$ Query = mysql_query ($ SQL );
$ Rs = mysql_fetch_row ($ query );
If ($ rs [6] & ADD ){
Echo "with permission ";
} Else {
Echo "no permission ";
}
?>

Here we mainly use the binary method. This is a way of thinking. Therefore, when defining a constant, it must be 2 to the Npower.
<? Php
Define ('add', 1); // 1 converts binary
Define ('upd', 2); // 10
Define ('Lis ', 4); // 100
Define ('del ', 8); // 1000

$ Sy = ADD | UPD | LIS | DEL;
Echo "All Permissions". decbin ($ sy );
$ Ny = $ sy ^ (UPD | DEL );
Echo "You are not authorized to delete or update". decbin ($ ny );

In PHP, you can use the & operator to determine whether a number is within a certain number range.
& Bits, not & logical operations (bit operator: & | ^ ~)
Example: 7 = 4 | 2 | 1 = "$ sy = 4 | 2 | 1
Here, the content value that can be included can be simply understood as 7 = 4 + 2 + 1 in the bitwise operation,
You can know that 7 & 4, 7 & 2, 7 & 1 are true, and 7 & 8 is false.
We can also use ^ to remove the contained values, for example, $ ny = $ sy ^ 2.
In this way, $ ny only has 4 | 1. If you make a judgment, only $ ny & 4, $ ny & 1 will be available.

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.