【问题标题】:Php site root urlphp站点根地址
【发布时间】:2011-05-27 11:59:06
【问题描述】:

我正在使用将项目详细信息提交给贝宝通过贝宝付款。 这里我需要指定我的 notify.php 的链接。 为此,我需要动态获取我的站点根目录。 我怎样才能得到它。

我的系统文件夹结构是 C:\wamp\www\website\store_esp\notify.php

我的 notify.php 网址应该是 http://domainname/store_esp/notify.php

目前我的域名是http://localhost/website/

如何使用php动态获取域名。

【问题讨论】:

  • 域名为localhost。但网站只是一个目录。去图

标签: php paypal payment-processing


【解决方案1】:

此 PHP 函数返回完整路径的真实 URL。

function pathUrl($dir = __DIR__){

    $root = "";
    $dir = str_replace('\\', '/', realpath($dir));

    //HTTPS or HTTP
    $root .= !empty($_SERVER['HTTPS']) ? 'https' : 'http';

    //HOST
    $root .= '://' . $_SERVER['HTTP_HOST'];

    //ALIAS
    if(!empty($_SERVER['CONTEXT_PREFIX'])) {
        $root .= $_SERVER['CONTEXT_PREFIX'];
        $root .= substr($dir, strlen($_SERVER[ 'CONTEXT_DOCUMENT_ROOT' ]));
    } else {
        $root .= substr($dir, strlen($_SERVER[ 'DOCUMENT_ROOT' ]));
    }

    $root .= '/';

    return $root;
}

此文件中 pathUrl 的调用:http://example.com/shop/index.php

#index.php

echo pathUrl();
//http://example.com/shop/

使用别名:http://example.com/alias-name/shop/index.php

#index.php

echo pathUrl();
//http://example.com/alias-name/shop/

子目录:http://example.com/alias-name/shop/inc/config.php

#config.php

echo pathUrl(__DIR__ . '/../');
//http://example.com/alias-name/shop/

https://stackoverflow.com/a/36101073/3626097

【讨论】:

    【解决方案2】:

    $_SERVER['HTTP_HOST']会给出域名

     http://localhost/website
    

    在上面的 URL 中,域是 localhost 我认为website 永远不会更改为website1website2,因此您可以在您的网址中静态提及这一点。

    【讨论】:

    【解决方案3】:

    使用这个

    http://".$_SERVER['HTTP_HOST']."/store_esp/
    

    然后使用notify.php之类的文件名

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多