2010-12-18 67 views
1

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

我的系统文件夹结构 C:\ WAMP \ WWW \网站\ store_esp \ notify.php

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

目前我domail名字是http://localhost/website/

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

+0

域名是'localhost'。但网站只是一个目录。去图 – 2010-12-18 07:41:29

回答

4

$_SERVER['HTTP_HOST']会给域名

http://localhost/website 

在上面的URL中的域名是localhost 当我想到了website是永远不会改变website1website2这样你就可以在你的URL静态提到这一点。

+0

我试过这个 http://'.$_SERVER ['HTTP_HOST']。'/ store_esp/notify.php?status = T 但是它给的 http://localhost/store_esp/notify.php ?status = F 但我需要 http://localhost/website/store_esp/notify.php?status = F – 2010-12-18 07:21:24

+0

检查更新的答案可能对您有用 – 2010-12-18 07:29:55

+0

。你以前的回答是正确的..我需要使用$ _SERVER ['HTTP_HOST']。当它将在现场时,它会没事的。 所以请回复你的答案。实际上第二个1就像做任何事情都是完全的。感谢你的回答。 – 2010-12-18 07:52:42

4

使用本

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

并在此之后使用文件名类似notify.php

0

这个PHP函数返回一个完整路径的真实URL。在这个文件中pathUrl的

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

电话: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