百搜论坛欢迎您的加入!
adimg adimg
 
昨日:篇  今日:篇   总帖:篇   会员:
博主最大Lv63   
thinkphp5配置指定ip访问模块     

在config.php中添加

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

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

最好加在这个位置

1

2

3

4

5

6

// 禁止访问模块

'deny_module_list'       => ['common'],

// 设置某些ip可以访问指定模块

'allow_module_ip'        => ['admin' => '*'],

// 默认控制器名

'default_controller'     => 'Index',

需要修改框架代码
thinkphp/library/think/App.php

代码位置如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

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);

        }

         

        // ......

}

推荐教程:《TP5

以上就是thinkphp5配置指定ip访问模块的详细内容

 0  已被阅读了1031次  楼主 2020-06-23 09:34:39
回复列表

回复:thinkphp5配置指定ip访问模块

联系站长 友链申请桂ICP备19000949号-1     桂ICP备19000949号-1
您的IP:3.135.207.129,2024-05-05 11:57:01,Processed in 0.02569 second(s).
免责声明: 本网不承担任何由内容提供商提供的信息所引起的争议和法律责任。
Powered by HadSky 7.12.9
已有0次打赏
(0) 分享
分享
取消
免责声明
1、本站资源,均来自网络,版权归原作者,所有资源和文章仅限用于学习和研究目的 。
2、不得用于商业或非法用途,否则,一切责任由该用户承担 !
如果觉得本文还不错请点个赞或者打赏点轻币哦~
拒绝伸手党,拿走请回复,尊重楼主,尊重你我他~

侵权删除请致信 E-Mail:207882320@qq.com