php web用户权限,WEB服务端权限控制

在项目中很多需要用到权限的地方,整理了些我常用到的权限控制。 1. IP限制及计数 描述:绑定指定的IP可访问,并对IP访问次数记录 流程如下: 1.1) 代码实现如下: if($user = Statistical::check_user($_SERVER["REMOTE_ADDR"])) { $statistical = new Sta

在项目中很多需要用到权限的地方,整理了些我常用到的权限控制。

1. IP限制及计数

描述:绑定指定的IP可访问,并对IP访问次数记录

流程如下:

ef758a5b01d08bb6a8c6539949287b7f.png

1.1) 代码实现如下:

if($user = Statistical::check_user($_SERVER["REMOTE_ADDR"])) {

$statistical = new Statistical($user,$query_flg); //$query_flg为接口的标识

if(!$statistical -> check_visits()) {

//验证访问次数是否达到限制

echo '访问次数超过限制!';exit;

}

//获取数据

$data = getData();

return $data;

} else {

//无效用户

echo '无访问权限!';exit;

}

1.2)表的关系:

a70e1f34aeff7b173e0003b8f5622d1c.png

1.3) 说明

check_user()方法即通过ip在ip_maping表中查找用户,若找到对应用户则在user_visit_config表中校验此用户是否还可以访问接口

校验步骤可分为:

1.3.1) 若用户第一次访问,插入记录,设置默认访问次数及有效期

1.3.2) 若不是第一次访问,若未过期:

A.访问次数没有超过当天上限,计数+1

B.访问次数超过当天上限,若为次天,则访问次数重新归1

(注:这里是每天都有固定的次数限制,超过则不可访问,要等到次天重新归1)

1.4)代码片段

/**

* 用户检查,根据ip判断用户是否存在

*/

public static function check_user($ip) {

$IPMappingQ = new IPMappingQuery();

$IPMappingQ -> setIp($ip);

$ipMapping = $IPMappingQ -> queryOne();

if($ipMapping != null) {

return $ipMapping -> user;

}

return null;

}

/**

* 校验此用户是否可以访问接口

*/

public function check_visits() {

try {

$counterQ = new CounterQuery();

$counterQ -> setUser($this->user_flg);

$counterQ -> setInterface_flg($this->flg);

if($counterQ -> queryCount() == 0) {

//初次访问,插入记录

//(这是我根据业务实现的一种逻辑,有些时候最好先将用户可访问的接口在数据库中配置好,

若未查询到这个用户对应接口则返回false)

$counterInfo = new CounterInfo();

$counterInfo -> setUser($this->user_flg);

$counterInfo -> setInterface_flg($this->flg);

$counterInfo -> setVisits('1');

$counterInfo -> setVisits_limit(DEFAULT_LIMIT);

$counterInfo -> setValid_date(DEFAULT_VALID);

$counterInfo -> setInsert_time(ComFunction::getNowDateTime());

$counterInfo -> setUpdate_time(ComFunction::getNowDateTime());

$counterInfo -> save();

return true;

}else {

$counter = $counterQ -> queryOne();

if(ComFunction::compareDateTimeStr(ComFunction::addDate($counter->getInsert_time(), $counter->getValid_time()),

$counter->getUpdate_time(), '>=')) { //判断是否过期

if($counter -> getVisits() < $counter -> getVisits_limit()) {

$counter -> setVisits($counter->getVisits()+1);

$counter -> setUpdate_time(ComFunction::getNowDateTime());

$counter -> save();

return true;

}else {

$last_update_date = substr($counter -> getUpdate_time(),0,10);

if(ComFunction::compareDateTimeStr(ComFunction::nowDate(), $last_update_date, ' setVisits('1');

$counter -> setUpdate_time(ComFunction::getNowDateTime());

$counter -> save();

return true;

}

}

}

}

} catch (Exception $e) {

return false;

}

return false;

}

2. 根据用户权限加载相应的模块

描述:即可指定不同的用户查看不同的功能模块。

需求:现在有一个树状的功能模块列表,每个模块都有一个对于的ID,有3种角色:超级管理员,普通管理员,一般用户;可通过配置指定角色查看指定的功能,每种角色可有多个用户。

先说说思路:有一个用户表,用户登录成功后,根据其用户名查找出用户所在用户组,根据此用户组找到此用户组可查看的模块ID列表,最后根据模块ID加载模块。

表的设计:

465a73d5d340e645380fb110a12f5a32.png

同样,在做接口权限访问时也可以使用此思路,即指定某个用户组可访问指定的方法,

新建一个用户组user_group1, 将可访问的方法名(对应model_id)录入到数据库,再为这个用户组生成一个密钥(对应user_name,user_pwd), 用户可通过此密钥访问对应的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值