获取 PHP 网站的根 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19721138/
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
Get root URL for a PHP website
提问by allejo
I'm currently working on a PHP project and am looking for a way to get the URL for the root of the website; I have a configuration file at the root so I'm thinking as to using that to figure out the "base URL." I'm looking for a way to do it dynamically so I can locate the URL of the root of the website, i.e. http://domain.com/my_app/
. I am doing my best to avoid using relative paths and am using PHP to generate the URLs for whatever I am using. For example, I am using PHP to generate CSS code and the CSS code link to images so I would like to get an absolute URL in here instead of a relative path.
我目前正在开发一个 PHP 项目,正在寻找一种方法来获取网站根目录的 URL;我在根目录有一个配置文件,所以我想用它来找出“基本 URL”。我正在寻找一种动态执行此操作的方法,以便我可以找到网站根目录的 URL,即http://domain.com/my_app/
. 我正在尽最大努力避免使用相对路径,并且正在使用 PHP 为我正在使用的任何内容生成 URL。例如,我使用 PHP 生成 CSS 代码和图像的 CSS 代码链接,因此我想在此处获取绝对 URL 而不是相对路径。
my_app/
admin/
resources/
css/
admin-css.php
imgs/
login.png
resources/
css/
css.php
imgs/
my-image.png
shared-image.png
config.php
In my resources/css/css.php
file, I am looking at getting the "base URL" so I can generate an absolute URL to the imgs
folder like http://domain.com/resources/imgs/my-image.png
but currently I am getting http://domain.com/resources/css/imgs/my-image.png
since the defines below look at getting the directory of the loaded PHP file, not the included one. I would also like to share images between the folders (i.e. access the shared-image.png
file from the admin
folder) so getting the base URL would be ideal in generating links. The reason I an avoiding relative paths is because I have a function that creates a URL, createURL()
, so I can get all the URLs working without having to hard code anything.
在我的resources/css/css.php
文件中,我正在查看获取“基本 URL”,以便我可以生成imgs
文件夹的绝对 URL,http://domain.com/resources/imgs/my-image.png
但目前我正在获取,http://domain.com/resources/css/imgs/my-image.png
因为下面的定义查看获取加载的 PHP 文件的目录,而不是包含的目录。我还想在文件夹之间共享图像(即shared-image.png
从admin
文件夹访问文件),因此获取基本 URL 将是生成链接的理想选择。我避免使用相对路径的原因是因为我有一个创建 URL 的函数createURL()
,所以我可以让所有 URL 工作而无需硬编码任何东西。
<?php
DEFINE('HTTP_TYPE', $_SERVER['HTTP_X_FORWARDED_PROTO']);
DEFINE('HTTP_ROOT', $_SERVER['HTTP_HOST']);
DEFINE('HTTP_FOLDER', dirname($_SERVER['PHP_SELF']) . '/');
DEFINE('BASE_URL', HTTP_TYPE . "://" . HTTP_ROOT . HTTP_FOLDER);
function createURL($pathFromRoot)
{
return BASE_URL . $pathFromRoot;
}
All of these defines are located in my configuration file, so I am thinking that the best way to do this is to get the URL for the config file (http://domain.com/my_app/config.php
) and just strip the "config.php." Keep in mind, the website could be hosted deeper in a folder structure http://domain.com/my_app/another/folder/config.php
or no sub folders http://domain.com/config.php
.
所有这些定义都位于我的配置文件中,所以我认为最好的方法是获取配置文件 ( http://domain.com/my_app/config.php
)的 URL并删除“config.php”。请记住,该网站可以托管在文件夹结构的更深处,也可以不托管http://domain.com/my_app/another/folder/config.php
子文件夹http://domain.com/config.php
。
Is this possible, if so how can it be done? Or is there another approach I should follow?
这是可能的,如果可以,怎么做?或者我应该遵循另一种方法吗?
回答by allejo
After doing a var_dump
of $_SERVER
and messing around I found that I could generate the base URL with this solution, in case anyone stumbles upon this question. I'm sure there are other ways but this is what worked for me.
做了之后var_dump
的$_SERVER
和乱搞我发现我能产生这种解决方案在这个问题上的基本URL,以防有人绊倒。我确定还有其他方法,但这对我有用。
<?php
DEFINE('BASE_URL', HTTP_TYPE . "://" . HTTP_ROOT . substr(__DIR__, strlen($_SERVER[ 'DOCUMENT_ROOT' ])) . '/');
回答by CrandellWS
I just needed the same thing I combined the answers from hereand hereto create this solution which can be easily converted to multiple constants and/or a function. I use a single entry point for my web application (index.php) and have my config in a sub-folder. You can either strip the known folder as shown in the commented line of code or run this from the main folder. As example your structure could be either/or of:
我只需要相同的东西,我将这里和这里的答案结合起来,以创建这个可以轻松转换为多个常量和/或函数的解决方案。我为我的 Web 应用程序 (index.php) 使用单个入口点,并将我的配置放在一个子文件夹中。您可以删除代码注释行中显示的已知文件夹,也可以从主文件夹运行它。例如,您的结构可能是/或:
- /public/html/some/random/folders/THEAPP/config.php
$urlDir = str_replace($docRoot, '', $dirRoot);
- /public/html/some/random/folders/THEAPP/config.php
$urlDir = str_replace($docRoot, '', $dirRoot);
or
或者
- /public/html/some/random/folders/THEAPP/include/config.php
$urlDir = str_replace('/include', '/',str_replace($docRoot, '', $dirRoot));
- /public/html/some/random/folders/THEAPP/include/config.php
$urlDir = str_replace('/include', '/',str_replace($docRoot, '', $dirRoot));
Entire Code:
完整代码:
$domain = $_SERVER['HTTP_HOST'];
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$dirRoot = dirname(__FILE__);
$protocol = isset($_SERVER["HTTPS"]) ? 'https://' : 'http://';
//$urlDir = str_replace('/include', '/',str_replace($docRoot, '', $dirRoot));
$urlDir = str_replace($docRoot, '', $dirRoot);
$site_path = $protocol.$domain.$urlDir;
define ('BASE_URL', $site_path);
Based server configurations: The output may not contain a trailing slash / if wanted then use either of:
基于服务器配置:输出可能不包含尾部斜杠/如果需要,请使用以下任一项:
$urlDir = str_replace('/include', '/',str_replace($docRoot, '/', $dirRoot));
$urlDir = str_replace($docRoot, '/', $dirRoot);
$urlDir = str_replace('/include', '/',str_replace($docRoot, '/', $dirRoot));
$urlDir = str_replace($docRoot, '/', $dirRoot);
BTW, the function seems unnecessary as example:
顺便说一句,该功能似乎没有必要,例如:
function createURL($pathFromRoot)
{
return BASE_URL . $pathFromRoot;
}
//Anywhere you can call `createURL($value)` makes the following true
$isTrue = createURL('/static/js/my.js') === BASE_URL.'/static/js/my.js';
$someNeededURL = '/static/whatever/is/converted/';
$alsoTrue = createURL($someNeededURL) === BASE_URL.$someNeededURL;
//and just for fun
if ($isTrue.$alsoTrue === 11){
echo 'Not eleven';
} else {
echo $isTrue.$alsoTrue.'!==11';
};