如何找到应用程序的基本网址?

2021-12-28 00:00:00 web-applications php

我想找到我的应用程序的基本 url,以便我可以自动引用我的应用程序树中的其他文件...

I'd like to find the base url of my application, so I can automatically reference other files in my application tree...

因此,在我的应用程序的基础中给定一个文件 config.php,如果子目录中的文件包含它,则知道在 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.

application/config.php
application/admin/something.php
application/css/style.css

因此,鉴于访问了 http://www.example.com/application/admin/something.php,我希望它能够知道 css 文件在 中$approot/css/style.css.在这种情况下,$approot/application",但我希望它知道该应用程序是否安装在其他地方.

So given that http://www.example.com/application/admin/something.php is accessed, I want it to be able to know that the css file is in $approot/css/style.css. In this case, $approot is "/application" but I'd like it to know if the application is installed elsewhere.

我不确定这是否可能,许多应用程序(我认为是 phpMyAdmin、Squirrelmail)必须首先设置一个配置变量.如果它只是知道它会更加用户友好.

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.

推荐答案

我在 homebrew 框架中使用以下内容...将其放在应用程序根文件夹中的文件中并简单地包含它.

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);

上面的代码定义了两个常量.ABSPATH 包含应用程序根目录(本地文件系统)的绝对路径,而 URLADDR 包含应用程序的完全限定 URL.它确实适用于 mod_rewrite 情况.

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.

相关文章