溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

thinkphp5如何增加允許指定ip訪(fǎng)問(wèn)模塊

發(fā)布時(shí)間:2021-01-16 10:37:42 來(lái)源:億速云 閱讀:196 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)thinkphp5如何增加允許指定ip訪(fǎng)問(wèn)模塊,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在config.php中添加

'allow_module_ip' => ['admin' => '*'], // 設(shè)置某些ip可以訪(fǎng)問(wèn)指定模塊

['admin' => '*'] 所有ip都可以訪(fǎng)問(wèn)admin模塊,

['admin' => ['127.0.0.1','192.168.1.100']] 僅這兩個(gè)ip可以訪(fǎng)問(wèn)admin模塊

最好加在這個(gè)位置

// 禁止訪(fǎng)問(wèn)模塊
'deny_module_list'       => ['common'],
// 設(shè)置某些ip可以訪(fǎng)問(wèn)指定模塊
'allow_module_ip'        => ['admin' => '*'],
// 默認(rèn)控制器名
'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 設(shè)置了限制ip訪(fǎng)問(wèn)模塊, 如:'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);
                // 模塊請(qǐng)求緩存檢查
                $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);
        }
        
        // ......
}

關(guān)于“thinkphp5如何增加允許指定ip訪(fǎng)問(wèn)模塊”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI