什么时候使用 $_SERVER['SCRIPT_URL'] 全局变量?

问题描述 投票:0回答:1

我一直在尝试从网址

/forgot_password
获取
https://lucasfrank.devlock/forgot_password?done=1
部分。我创建了下面的函数:

function getBaseURI(): string
{
    $basePath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
    // Get the current Request URI and remove rewrite base path from it
    // (= allows one to run the router in a sub folder)
    $uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen($basePath));
    // Don't take query params into account on the URL
    if (str_contains($uri, '?')) {
        $uri = substr($uri, 0, strpos($uri, '?'));
    }
    // Remove trailing slash + enforce a slash at the start
    return '/' . trim($uri, '/');
}

它可以在本地运行,但似乎是域名托管上的问题。后来我遇到了

$_SERVER['SCRIPT_URL']
> 全局变量,它与上面的函数一样完美。我使用了全局变量并且在域上工作得很好,但似乎在本地无效。

我预计这对两端都有效,但似乎这是不可能的。全局变量似乎时断时续,不确定何时可以访问。我在本地主机上访问它时遇到此错误:

警告:C:\larragon\www dmin_dashboard unctions unctions.php 第 13 行中未定义数组键“SCRIPT_URL”

我不得不更改为旧功能,不知道何时使用或不使用。我不知道我的功能缺少什么或者我可能做错了什么。

php server global-variables
1个回答
0
投票

您应该避免自己解析 URI,因为应该有各种语言的库来执行此操作:

parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
© www.soinside.com 2019 - 2024. All rights reserved.