doulu1945 2014-04-14 08:52
浏览 37

PHP Url相对和绝对

I have this class:

class PageBuilder {
    public function GetHeader() {
        include(dirname(dirname(__FILE__)) . '/template/header.php');

Which when called will insert the header file into my page. All is good.


In the header.php is the top of the HTML file which includes a menu. My problem is, depending on where the PageBuilder gets called from changes the menu link URLs.

How do I make sure they are always relative to the root folder.

dirname(__FILE__) doesn't work because it turns the URL into file:///, also I really don't want to append the entire because if it is relative to the root it doesn't matter about any of that.


So as someone posted you can use $_SERVER[''] but which one is reliable, no doubt PHP will have put in some blinding caveats.

See I was thinking $_SERVER['SERVER_ADDR'] or $_SERVER['SERVER_NAME'] or $_SERVER['HTTP_HOST'] .....

Going $_SERVER['HTTP_HOST'] for now .. result is fail.

Just prefixing with / resolves to the localhost root so my links are localhost/admin/profile.php rather than localhost/TestApp/admin/profile.php, Do I have to actually specify that it sits in folder named TestApp?

  • 写回答

2条回答 默认 最新

  • dongya2029 2014-04-14 09:33

    After trial and error with the help of @deceze I simply pass my URL through a routing class which prefixes the URL with '/TestApp/' this can be change easily if the project gets moved because the use of classes.

    So I do:

    <a href="<?php $url_routing->ParseUrl('admin/account/logout.php') ?>">Log Out </a>

    Gets passed through:

    public function ParseUrl($url) {
        return '/TestApp/' . $url;

    It is hardly best practice, but there can't be best practices with a language that doesn't follow any itself.

    Looking at @Anthony answer of using $_SERVER['PHP_SELF']; it does always give the TestApp folder. Needs more looking into but I will take that. I assume its relative to the server address.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dongzilu0178 2014-04-14 09:26

    You need to differentiate between local file paths and URLs first and foremost. The two have nothing to do with one another. PHP with its __FILE__ and dirname() and related parts is dealing only with local file paths on the server's hard disk. URLs on the other hand are entirely arbitrary strings that you need to put together in a sensible way. There are an infinite ways how any one particular file may be referred to by a URL, it's up to you to bring order into this.

    You could try to figure out which URL was requested using the various paths available in $_SERVER and construct relative URLs based on that. However, this is rather error prone and overly complicated. Rather you should be using root-relative URLs. I.e. instead of


    which is relative to the URL the browser is currently on, you always use


    which always refers to the same URL regardless of what URL it's currently relative to.

    If you need a prefix for all your URLs, I'd create a wrapper function which adds this as needed based on configuration or on automatically figuring out its own root URL based on $_SERVER.




  • ¥20 C语言字符串不区分大小写字典排序相关问题
  • ¥15 关于#python#的问题:我希望通过逆向技术爬取1688搜索页下滑加载的数据
  • ¥15 学习C++过程中遇到的问题
  • ¥15 关于Linux的终端里,模拟实现一个带口令保护的屏保程序遇到的输入输出的问题!(语言-c语言)
  • ¥15 学习C++过程中遇到的问题
  • ¥15 请问,这个嵌入式Linux系统怎么分析,crc检验区域在哪
  • ¥15 二分类改为多分类问题
  • ¥15 Unity微信小游戏上调用ReadPixels()方法报错
  • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
  • ¥15 q从常量变成sin函数,怎么改写python代码?