<? 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.