对于一个网站的后台管理系统,单一的超级管理员权限往往不能满足我们的需求,尤其是对于大型网站而言,这种单一的权限会引发许许多多的问题出现。
比如:一个网站编辑,平时他只是负责公司网站的公告更新,但如果网站后台没有严格的权限限制,他是不是就可以操作到客户的一些信息,这是存在很大隐患的。
如果学过ThinkPHP框架的朋友一定知道有个东西叫RBAC,今天咱不说那个,来说说在原生PHP语言中,怎么实现交叉权限控制。
好了,话不多说,老样子,直接说原理,上代码。
对于权限的交叉控制可以有很多种方法实现,这里只是提供一种思路:(我采用的是二进制数的方法)
一、这里先提下按位与和按位或的运算方法:
参加运算的两个数据,按二进制位进行“与”运算。
运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1;
即:两位同时为“1”,结果才为“1”,否则为0
例如:3&5 即 0000 0011 & 0000 0101 = 0000 0001 因此,3&5的值得1。
另,负数按补码形式参加按位与运算。
参加运算的两个对象,按二进制位进行“或”运算。
运算规则:0|0=0; 0|1=1; 1|0=1; 1|1=1;
即 :参加运算的两个对象只要有一个为1,其值为1。
例如:3|5 即 0000 0011 | 0000 0101 = 0000 0111 因此,3|5的值得7。
另,负数按补码形式参加按位或运算。
了解了按位与和按位或的运算,我们来看下面这个例子:
<span> 1</span> <?<span>php </span><span> 2</span> <span>define</span>('ADD',1);<span>//</span><span>二进制1</span> <span> 3</span> <span>define</span>('DELETE',2);<span>//</span><span>二进制10</span> <span> 4</span> <span>define</span>('UPDATE',4);<span>//</span><span>二进制100</span> <span> 5</span> <span>define</span>('SELECT',8);<span>//</span><span>二进制1000 </span><span> 6</span> <span> 7</span> <span> //有权限为1,没有权限为0</span> <span> 8</span> <span>$admin</span>=ADD|DELETE|UPDATE|SELECT;<span>//</span><span>1111</span> <span> 9</span> <span>$editer</span>=ADD|UPDATE|SELECT;<span>//</span><span>1101</span> <span>10</span> <span>$user</span>=SELECT;<span>//</span><span>1000</span> <span>11</span> ?>
我把增删改查分别做成了4个权限并定为常量
1的二进制数是1,2的二进制数是10,4的二进制数是100,8的二进制数是1000,这里刚好成一个规律
可能有些朋友会问上面权限变量admin,editer,user所对应的1111,1101,1000是怎么来的?
PHP里有一个十进制数转二进制数的函数叫decbin()
下面是对应的函数解释:
<span>decbin</span><span> (PHP </span>3, PHP 4, PHP 5<span>) </span><span>decbin</span> --<span> 十进制转换为二进制 说明 </span><span>string</span> <span>decbin</span> ( int <span>number</span><span> )<br /><br /> 返回一字符串,包含有给定 </span><span>number</span> 参数的二进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 32 个 1<span> 的字符串。 例子 </span>1. <span>decbin</span><span>() 范例 </span><?<span>php </span><span>echo</span> <span>decbin</span>(12) . "\n"<span>; </span><span>echo</span> <span>decbin</span>(26<span>); </span>?><span> 上例将输出: </span>1100 11010<span> 参见 </span><span>bindec</span>(),<span>decoct</span>(),<span>dechex</span>() 和 <span>base_convert</span>()。
我们来测试输出看看吧:
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span> 4</span> <span>define</span>('ADD',1);<span>//</span><span>二进制1</span> <span> 5</span> <span>define</span>('DELETE',2);<span>//</span><span>二进制10</span> <span> 6</span> <span>define</span>('UPDATE',4);<span>//</span><span>二进制100</span> <span> 7</span> <span>define</span>('SELECT',8);<span>//</span><span>二进制1000 </span><span> 8</span> <span> 9</span> <span> //有权限为1,没有权限为0</span> <span>10</span> <span>$admin</span>=ADD|DELETE|UPDATE|SELECT;<span>//</span><span>1111</span> <span>11</span> <span>$editer</span>=ADD|UPDATE|SELECT;<span>//</span><span>1101</span> <span>12</span> <span>$user</span>=SELECT;<span>//</span><span>1000</span> <span>13</span> <span>14</span> <span>echo</span> <span>decbin</span>(<span>$admin</span>)."<br/>"<span>; </span><span>15</span> <span>echo</span> <span>decbin</span>(<span>$editer</span>)."<br/>"<span>; </span><span>16</span> <span>echo</span> <span>decbin</span>(<span>$user</span>)."<br/>"<span>; </span><span>17</span> <span>18</span> <span>19</span> ?>
输出结果:
那么我们就可以运用这个运算来判断权限了,1代表有权限,0代表无权限
比如:
admin(超级管理员)拥有的权限是增删改查也就是1111——>0000 1111
editer(网站编辑)拥有的权限是增,改,查也就是1101——>0000 1101
user(普通用户)只拥有浏览、查询的权限也就是1000——>0000 1000
那么我们只要对它们进行按位与运算就可以判断是否具备权限了
例如:
网站编辑权限 0000 1101 | 0000 0010(删除权限十进制为2转二进制为10) 结果:0000 0000 也就是没有具备权限
再来试试
普通用户权限 0000 1000 |0000 0001(添加权限十进制为1二进制为1) 结果:0000 0000 也一样不具备权限
超级管理员权限0000 1111 |0000 1101(网站编辑的权限) 结果:0000 1111 也就是具备了网站编辑的权限
好了看具体实例吧
我建了一个数据库,里面有2张表
一张是user用户表:
gid代表权限表的组id
一张是权限表:
flag代表增删改查的权限,可根据自己需要定义
基本配置页面:config.php
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>define</span>('HOST','localhost'<span>); </span><span> 4</span> <span>define</span>('DBNAME','member'<span>); </span><span> 5</span> <span>define</span>('USER', 'root'<span>); </span><span> 6</span> <span>define</span>('PASS', ''<span>); </span><span> 7</span> <span> 8</span> <span> 9</span> <span>$link</span>=@<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>('数据库连接失败'<span>); </span><span>10</span> <span>11</span> <span>mysql_select_db</span>(DBNAME,<span>$link</span><span>); </span><span>12</span> <span>13</span> <span>define</span>('ADD',1);<span>//</span><span>二进制1</span> <span>14</span> <span>define</span>('DELETE',2);<span>//</span><span>二进制10</span> <span>15</span> <span>define</span>('UPDATE',4);<span>//</span><span>二进制100</span> <span>16</span> <span>define</span>('SELECT',8);<span>//</span><span>二进制1000 </span><span>17</span> <span>18</span> <span> //有权限为1,没有权限为0</span> <span>19</span> <span>$admin</span>=ADD|DELETE|UPDATE|SELECT;<span>//</span><span>1111</span> <span>20</span> <span>$editer</span>=ADD|UPDATE|SELECT;<span>//</span><span>1101</span> <span>21</span> <span>$user</span>=SELECT;<span>//</span><span>1000</span> <span>22</span> ?>
登陆首页:index.html
<span> 1</span> <span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span> <span> 2</span> <span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span><span> xml:lang</span><span>="en"</span><span>></span> <span> 3</span> <span><</span><span>head</span><span>></span> <span> 4</span> <span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html;charset=UTF-8"</span><span>></span> <span> 5</span> <span><</span><span>title</span><span>></span>Document<span></</span><span>title</span><span>></span> <span> 6</span> <span></</span><span>head</span><span>></span> <span> 7</span> <span><</span><span>body</span><span>></span> <span> 8</span> <span><</span><span>form </span><span>action</span><span>="action.php"</span><span> method</span><span>="post"</span><span>></span> <span> 9</span> 账号:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="username"</span> <span>/></span> <span>10</span> 密码:<span><</span><span>input </span><span>type</span><span>="password"</span><span> name</span><span>="password"</span> <span>/></span> <span>11</span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="登陆"</span><span>></span> <span>12</span> <span></</span><span>form</span><span>></span> <span>13</span> <span></</span><span>body</span><span>></span> <span>14</span> <span></</span><span>html</span><span>></span>
提交页面:action.php
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>require_once</span>('config.php'<span>); </span><span> 4</span> <span>$username</span>=<span>$_POST</span>['username'<span>]; </span><span> 5</span> <span>$password</span>=<span>$_POST</span>['password'<span>]; </span><span> 6</span> <span> 7</span> <span> 8</span> <span>$sql</span>="<span>select * from user as a,role as b where a.gid=b.gid </span><span> 9</span> and a.username='<span>$username</span>' and password='<span>$password</span>'"<span>; </span><span>10</span> <span>11</span> <span>$result</span>=<span>mysql_query</span>(<span>$sql</span><span>); </span><span>12</span> <span>if</span>(<span>$data</span>=<span>mysql_fetch_array</span>(<span>$result</span><span>)){ </span><span>13</span> <span>//</span><span>账号验证通过,判断对应权限 </span><span>14</span> <span> //此处判断的是 是否具备删除权限</span> <span>15</span> <span>if</span>(<span>$data</span>['flag']&<span>DELETE){ </span><span>16</span> <span>echo</span> "你有删除权限"<span>; </span><span>17</span> }<span>else</span><span>{ </span><span>18</span> <span>echo</span> "你没有删除权限"<span>; </span><span>19</span> <span> } </span><span>20</span> <span>21</span> }<span>else</span><span>{ </span><span>22</span> <span>echo</span> "错误账号密码"<span>; </span><span>23</span> <span> } </span><span>24</span> <span>25</span> <span>26</span> ?>
效果图如下:
轻松搞定~
这里只是个简单的小DEMO演示,希望能起到抛砖引玉的作用,至于具体项目还需具体分析,权限控制毕竟是个很复杂的功能。
不明白.,,...
肯定会慢一点,但是这是可以接受的。因为采用框架造成的性能损失比较恒定,例如对于所有功能,使用框架和直编可能总是框架慢0.002毫秒。但是这种损失一般不需要在意。因为相对于框架的巨大好处,这种损耗是值得的。
利用框架可以大幅度提升开发效率
大幅度节约维护成本
更容易的项目交接
因此,宁可损失效率也选择框架。关于效率可以考虑升级服务器等手段来改善。