這篇文章主要介紹了thinkphp5如何增加允許指定ip訪問模塊配置,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
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);
}
// ......
}感謝你能夠認真閱讀完這篇文章,希望小編分享的“thinkphp5如何增加允許指定ip訪問模塊配置”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。