php 如何找到应用程序的基本网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/176712/
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
How can I find an application's base url?
提问by DGM
I'd like to find the base url of my application, so I can automatically reference other files in my application tree...
我想找到我的应用程序的基本 url,这样我就可以自动引用我的应用程序树中的其他文件...
So given a file config.php in the base of my application, if a file in a subdirectory includes it, knows what to prefix a url with.
因此,在我的应用程序的基础中给定一个文件 config.php,如果子目录中的文件包含它,则知道要为 url 加上什么前缀。
application/config.php
application/admin/something.php
application/css/style.css
So given that http://www.example.com/application/admin/something.phpis accessed, I want it to be able to know that the css file is in $approot/css/style.css. In this case, $approotis "/application" but I'd like it to know if the application is installed elsewhere.
所以考虑到它http://www.example.com/application/admin/something.php被访问,我希望它能够知道 css 文件在$approot/css/style.css. 在这种情况下,$approot是“ /application”,但我希望它知道应用程序是否安装在其他地方。
I'm not sure if it's possible, many applications (phpMyAdmin, Squirrelmail I think) have to set a config variable to begin with. It would be more user friendly if it just knew.
我不确定这是否可能,许多应用程序(我认为是 phpMyAdmin、Squirrelmail)必须先设置一个配置变量。如果它只是知道它会更加用户友好。
回答by Andrew Moore
I use the following in a homebrew framework... Put this in a file in the root folder of your application and simply include it.
我在自制框架中使用以下内容......将它放在应用程序根文件夹中的文件中,然后简单地包含它。
define('ABSPATH', str_replace('\', '/', dirname(__FILE__)) . '/');
$tempPath1 = explode('/', str_replace('\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\', '/', dirname($_SERVER['PHP_SELF'])));
for ($i = count($tempPath2); $i < count($tempPath1); $i++)
array_pop ($tempPath3);
$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);
if ($urladdr{strlen($urladdr) - 1}== '/')
define('URLADDR', 'http://' . $urladdr);
else
define('URLADDR', 'http://' . $urladdr . '/');
unset($tempPath1, $tempPath2, $tempPath3, $urladdr);
The above code defines two constants. ABSPATH contains the absolute path to the root of the application (local file system) while URLADDR contains the fully qualified URL of the application. It does work in mod_rewrite situations.
上面的代码定义了两个常量。ABSPATH 包含应用程序根目录(本地文件系统)的绝对路径,而 URLADDR 包含应用程序的完全限定 URL。它确实适用于 mod_rewrite 情况。
回答by Murtaza Baig
You can find the base url with the folowing code:
您可以使用以下代码找到基本网址:
define('SITE_BASE_PATH','http://'.preg_replace('/[^a-zA-Z0-9]/i','',$_SERVER['HTTP_HOST']).'/'.str_replace('\','/',substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']))).'/');
Short and best.
简短而最好。
回答by Jay
The REQUEST_URI combined with dirname()can tell you your current directory, relevant to the URL path:
REQUEST_URI 结合dirname()可以告诉您当前目录,与 URL 路径相关:
<?php
echo dirname($_SERVER["REQUEST_URI"]);
?>
So http://example.com/test/test.phpprints "/test" or http://example.com/prints "/" which you can use for generating links to refer to other pages relative to the current path.
因此http://example.com/test/test.php打印“/test”或http://example.com/打印“/”,您可以使用它来生成链接以引用相对于当前路径的其他页面。
EDIT: just realized on re-reading that you might be asking about the on-disk path as opposed to the URL path. In that case, you want PHP's getcwd()function instead:
编辑:刚刚在重新阅读时意识到您可能会询问磁盘路径而不是 URL 路径。在这种情况下,您需要 PHP 的getcwd()函数:
<?php
echo getcwd();
?>
回答by Lucas Oman
Put this in your config.php (which is in your app's root dir):
把它放在你的 config.php (在你的应用程序的根目录中):
$approot = substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']));
I think that'll do it.
我想就可以了。
回答by theraccoonbear
Unless you track this yourself, I don't believe this would have a definition. Or rather, you're asking PHP to track something that you're somewhat arbitrarily defining.
除非你自己跟踪这个,否则我不相信这会有一个定义。或者更确切地说,您是在要求 PHP 跟踪您随意定义的某些内容。
The long and short of it is, if I'm understanding your question correctly, I don't believe what you're asking for exists, at least not as "core" PHP; a given framework may provide a "root" URL relative to a directory structure that itunderstands.
总而言之,如果我正确理解您的问题,我不相信您所要求的东西存在,至少不是“核心”PHP;一个给定的框架可以提供一个相对于它理解的目录结构的“根”URL 。
Does that make sense?
那有意义吗?
回答by rakesh sadaka
url root of the application can be found by
可以通过以下方式找到应用程序的 url 根目录
$protocol = (strstr('https',$_SERVER['SERVER_PROTOCOL']) === false)?'http':'https';
$url = $protocol.'://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']);
this code will return url path of any application whether be online or on offline server.
此代码将返回任何应用程序的 url 路径,无论是在线还是离线服务器。
I've not made proper check for proper '/'. Please modify it for slashes.
我没有对正确的“/”进行适当的检查。请修改为斜杠。
this file should be placed in root.
这个文件应该放在根目录下。
example : if application url :
示例:如果应用程序网址:
http://localhost/test/test/test.php
then this code will return
然后此代码将返回
http://localhost/test/test
Thanks
谢谢
回答by Adam Pierce
One solution would be to use relative paths for everything, that way it does not matter where the app is installed. For example, to get to your style sheet, use this:
一种解决方案是对所有内容使用相对路径,这样安装应用程序的位置无关紧要。例如,要访问您的样式表,请使用以下命令:
../css/style.css
回答by UnkwnTech
If you want the path on the filesystem you can use $_SERVER['DOCUMENT_ROOT']
If you just want the path of the file that appears in the URL after the domain use $_SERVER['REQUEST_URI']
如果您想要文件系统上的路径,您可以使用$_SERVER['DOCUMENT_ROOT']
如果您只想要域使用后出现在 URL 中的文件的路径$_SERVER['REQUEST_URI']
回答by da5id
I've never found a way to make it so.
我从来没有找到一种方法来做到这一点。
I always end up setting a config variable with the server path to one level above web root. Then it's:
我总是最终将服务器路径的配置变量设置为 Web 根目录之上的一个级别。然后是:
- $configVar . 'public/whatever/' for stuff inside root,
- but you can also include from outside with $configVar . 'phpInc/db.inc.php/' etc.
- $configVar 。'public/whatever/' 用于 root 中的内容,
- 但您也可以使用 $configVar 从外部包含。'phpInc/db.inc.php/' 等
回答by SchizoDuckie
This works like a charm for me, anywhere I deploy, with or without rewrite rules:
这对我来说就像一个魅力,在我部署的任何地方,无论是否有重写规则:
$baseDir = 'http://'.$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']) != '/' ? dirname($_SERVER["SCRIPT_NAME"]).'/' : '/');
$baseDir = 'http://'.$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']) != '/' ? dirname($_SERVER["SCRIPT_NAME"]).'/' : '/');

