Home > php教程 > php手册 > body text

WEB服务端权限控制

WBOY
Release: 2016-06-06 20:12:15
Original
2085 people have browsed it

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

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

1. IP限制及计数

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

流程如下:

D{II5`B9NES~[FI(HG63I{C

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;
}
Copy after login

1.2)表的关系:

RQJ7AIJYR@9300BWXKL5X)M

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()  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;
}
Copy after login

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

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

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

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

表的设计:

M{S5AL51O}I_TU{]OOHH0AG

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!