1

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

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

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

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

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

4

3 回答 3

4

$_SERVER['HTTP_HOST']会给域名

 http://localhost/website

在上面的 URL 中,域是localhost 我认为website从未更改过的域website1website2因此您可以在您的 url 中静态提及这一点。

于 2010-12-18T07:16:13.517 回答
4

用这个

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

在此之后使用文件名,如 notify.php

于 2010-12-18T07:19:38.427 回答
0

这个 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

于 2016-03-19T11:37:54.157 回答