PHP:如何获取文件所在的URL?

时间:2022-01-22 08:57:31

I can't seem to find a super-global for this. Basically, if a PHP file is executed on, say, http://www.example.com/services/page.php, I would like to retrieve http://example.com/services. How do I achieve this?

我似乎无法为此找到超级全局。基本上,如果在http://www.example.com/services/page.php上执行PHP文件,我想检索http://example.com/services。我该如何实现这一目标?

3 个解决方案

#1


You can abuse dirname:

你可以滥用dirname:

<p>
<?php echo $_SERVER["HTTP_HOST"] . dirname($_SERVER["REQUEST_URI"]) ?>
</p>

#2


Take a look at $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI']. HTTP_HOST would contain the host name the resource was requested from an REQUEST_URI URI path and query that was requested.

看看$ _SERVER ['HTTP_HOST']和$ _SERVER ['REQUEST_URI']。 HTTP_HOST将包含从REQUEST_URI URI路径和请求的查询请求资源的主机名。

#3



$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')  ? "https://" : "http://";

echo $scheme . str_replace(basename(__file__), '', $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);


#1


You can abuse dirname:

你可以滥用dirname:

<p>
<?php echo $_SERVER["HTTP_HOST"] . dirname($_SERVER["REQUEST_URI"]) ?>
</p>

#2


Take a look at $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI']. HTTP_HOST would contain the host name the resource was requested from an REQUEST_URI URI path and query that was requested.

看看$ _SERVER ['HTTP_HOST']和$ _SERVER ['REQUEST_URI']。 HTTP_HOST将包含从REQUEST_URI URI路径和请求的查询请求资源的主机名。

#3



$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')  ? "https://" : "http://";

echo $scheme . str_replace(basename(__file__), '', $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);