Warning: include_once(/www/wwwroot/huison.cn/head.php): failed to open stream: Permission denied in /www/wwwroot/huison.cn/show.php on line 48

Warning: include_once(): Failed opening 'head.php' for inclusion (include_path='.:/www/server/php/56/lib/php') in /www/wwwroot/huison.cn/show.php on line 48

Warning: include_once(/www/wwwroot/huison.cn/banner.php): failed to open stream: Permission denied in /www/wwwroot/huison.cn/show.php on line 53

Warning: include_once(): Failed opening 'banner.php' for inclusion (include_path='.:/www/server/php/56/lib/php') in /www/wwwroot/huison.cn/show.php on line 53

PHP利用位与运算【&】实现简单权限设计


权限值是这样的:

读:2^0=1,相应2进数为”0001″(在这里^我表示成”次方”,即:2的0次方,下同)

写:2^1=2,相应2进数为”0010″

传:2^2=4,相应2进数为”0100″

删:2^3=8,相应2进数为”1000″


要判断一个数在某些数范围内就可以使用 & 运算符(数值从上面的表中得来),可以知道7&4、7&2、7&1都是为真,而如果7&8则是为假。


<?php
$key = 13;//13=8+4+1,二进制数为1101
if($key & 8)
echo '有删除权限<br>';
if($key & 4)
echo '有上传权限<br>';
if($key & 2)
echo '有写入权限<br>';
if($key & 1)
echo '有只读权限<br>';
?>


MySQL中也可使用位与运算(假如要读取所有具有上传权限的用户)



SELECT * FROM user WHERE `权限` & 4 > 0;

对于一般简单的权限设计这样使用足够了。

案例应用:DESTOON级别更改为可多选(checkbox)

其它相关文章:

Warning: include_once(/www/wwwroot/huison.cn/foot.php): failed to open stream: Permission denied in /www/wwwroot/huison.cn/show.php on line 126

Warning: include_once(): Failed opening 'foot.php' for inclusion (include_path='.:/www/server/php/56/lib/php') in /www/wwwroot/huison.cn/show.php on line 126