php PHP在服务器中获取网站根绝对URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10925493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:23:03  来源:igfitidea点击:

PHP get website root absolute URL in server?

php

提问by lisovaccaro

I rely heavily in $_SERVER["DOCUMENT_ROOT"]to get absolute paths. However this doesn't work for sites which URLs don't point to the root.

我非常依赖于$_SERVER["DOCUMENT_ROOT"]获得绝对路径。但是,这不适用于 URL 不指向根的站点。

I have sites stored in folders such as:

我的网站存储在文件夹中,例如:

  • site1
  • site2
  • 站点 1
  • 站点 2

all directly inside the root. Is there a way to get the path in the server where the current site root is?

所有直接在根内。有没有办法在服务器中获取当前站点根所在的路径?

It should return:

它应该返回:

   /var/chroot/home/content/02/6945202/html/site1 // If the site is stored in folder 'site1'
   /var/chroot/home/content/02/6945202/html // If the site is stored in the root

回答by Ja?ck

You can simply append dirname($_SERVER['SCRIPT_NAME'])to $_SERVER['DOCUMENT_ROOT'].

您可以简单地附加dirname($_SERVER['SCRIPT_NAME'])$_SERVER['DOCUMENT_ROOT'].

Update

更新

The website seems to be directory "mounted" on that folder, so SCRIPT_NAMEwill obviously be /.

该网站似乎在该文件夹上“挂载”了目录,因此SCRIPT_NAME显然/.

So, to make this work you have to use either __DIR__or dirname(__FILE__)to find out where your script is located in the file system.

因此,要完成这项工作,您必须使用__DIR__dirname(__FILE__)找出您的脚本在文件系统中的位置。

Update 2

更新 2

There's no single index.phpcontroller for the whole site, so that won't work either.

index.php整个站点没有单一的控制器,所以这也行不通。

The following expression does a string "subtraction" to find the common path. You have a known prefix (document root), an unknown (the root folder) and a known suffix (the script path), so to find the first two, you take the full absolute path (__FILE__) and subtract the known suffix:

以下表达式执行字符串“减法”以查找公共路径。您有一个已知的前缀(文档根目录)、一个未知的(根文件夹)和一个已知的后缀(脚本路径),因此要找到前两个,您需要使用完整的绝对路径 ( __FILE__) 并减去已知的后缀:

substr(__FILE__, 0, -strlen($_SERVER['SCRIPT_NAME']));

If included files need this value, you must store this in a constant first before including the dependent scripts. .

如果包含的文件需要此值,则必须先将其存储在常量中,然后再包含相关脚本。.

回答by Ergec

For future googlers, this also works for me

对于未来的谷歌员工,这也适用于我

substr(substr(__FILE__, strlen(realpath($_SERVER['DOCUMENT_ROOT']))), 0, - strlen(basename(__FILE__)));

回答by Ozzy

Just use getcwd()for the current absolute server path of the folder the current script is in.

getcwd()用于当前脚本所在文件夹的当前绝对服务器路径。

You can define a constant at the top of your website so that the rest of the website can rely on that path.

您可以在网站顶部定义一个常量,以便网站的其余部分可以依赖该路径。

define('MY_SERVER_PATH', getcwd());

回答by karthikeyan ganesan

use the following method to get the absolute root url from your server settings

使用以下方法从您的服务器设置中获取绝对根 url

   str_replace($_SERVER['DOCUMENT_ROOT']."/",$_SERVER['PHPRC'],$_SERVER['REAL_DOCUMENT_ROOT'])

or use the getcwd(). it gets the current working directory of your application

或使用getcwd()。它获取应用程序的当前工作目录