如何找到应用程序的基本URL? [英] How can I find an application's base url?

查看:1017
本文介绍了如何找到应用程序的基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到我的应用程序的基本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.

推荐答案

我在自制程序框架中使用以下内容...将其放在应用程序根文件夹中的文件中,并将其包含在内.

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.

这篇关于如何找到应用程序的基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆