thinkphp5增加允许指定ip访问模块的方法

作者 : OO资源中心 发布时间: 2020-05-11 文章热度:324 共1503个字,阅读需4分钟。 本文内容有更新 字体:

thinkphp5增加允许指定ip访问模块的方法

在config.php中添加

'allow_module_ip' => ['admin' => '*'], // 设置某些ip可以访问指定模块

['admin' => '*'] 所有ip都可以访问admin模块,

['admin' => ['127.0.0.1','192.168.1.100']] 仅这两个ip可以访问admin模块

最好加在这个位置

// 禁止访问模块
'deny_module_list'       => ['common'],
// 设置某些ip可以访问指定模块
'allow_module_ip'        => ['admin' => '*'],
// 默认控制器名
'default_controller'     => 'Index',

需要修改框架代码

thinkphp/library/think/App.php

代码如下

public static function module($result, $config, $convert = null) {
        if (is_string($result)) {
            $result = explode('/', $result);
        }
        $request = Request::instance();
        if ($config['app_multi_module']) {
            // 多模块部署
            $module    = strip_tags(strtolower($result[0] ?: $config['default_module']));
            $bind      = Route::getBind('module');
            $available = false;
            if ($bind) {
                // 绑定模块
                list($bindModule) = explode('/', $bind);
                if (empty($result[0])) {
                    $module    = $bindModule;
                    $available = true;
                } elseif ($module == $bindModule) {
                    $available = true;
                }
            } elseif (!in_array($module, $config['deny_module_list']) && is_dir(APP_PATH . $module)) {
                $available = true;
            }
            
            //region 设置了限制ip访问模块, 如:'allow_module_ip' => ['admin'=>['127.0.0.1']]
            if (isset($config['allow_module_ip']) && isset($config['allow_module_ip'][$module])) {
                $allowIps = $config['allow_module_ip'][$module];
                if (!in_array($_SERVER['REMOTE_ADDR'], $allowIps) && $allowIps != '*') {
                    $available = false;
                }

            }
            //end region

            // 模块初始化
            if ($module && $available) {
                // 初始化模块
                $request->module($module);
                $config = self::init($module);
                // 模块请求缓存检查
                $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']);
            } else {
                throw new HttpException(404, 'module not exists:' . $module);
            }
        } else {
            // 单一模块部署
            $module = '';
            $request->module($module);
        }
        
        // ......
}

《》


关注微信公众号『OOINK』

第一时间了解最新资源动态关注OO.INK不迷路~

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
下载资源需要解压密码,解压密码是什么?
如下载的资源需要解压密码,请查看下载页右侧查找解压密码;
如没有解压密码请尝试默认解压密码:www.oo.ink 或者 oo.ink
或者联系我们获取解压密码!
1、本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!

OO.INK资源网 » thinkphp5增加允许指定ip访问模块的方法
+已在风雨中度过
为兴趣而改变,为梦想而努力!
+已坚持更新
时时更新,确保最新~

开通SVIP,畅享全站资源下载~

立即了解