简体   繁体   English

PHP网站根网址

[英]Php site root url

I am doing a payment by paypal using submit the item details to paypal. 我正在使用Paypal进行付款,方法是将商品详细信息提交给Paypal。 Here I need to specify a link of my notify.php. 在这里,我需要指定一个notify.php的链接。 For this I need to get my site root dynamically. 为此,我需要动态获取网站根目录。 how can I get it. 我怎么才能得到它。

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

and my url for notify.php should be http://domainname/store_esp/notify.php 而我的notify.php网址应为http://domainname/store_esp/notify.php

Presently my domail name is http://localhost/website/ 目前,我的domail名称是http:// localhost / website /

How can I get the domainname dynamically using php. 如何使用php动态获取域名。

$_SERVER['HTTP_HOST'] will give the domain name $_SERVER['HTTP_HOST']将给出域名

 http://localhost/website

In the above URL the domain is localhost As I think the website is never changed to website1 or website2 so you can statically mention this in your url. 在上面的URL中,域是localhost因为我认为website永远不会更改为website1website2因此您可以在URL中静态提及。

use this 用这个

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

and after this use the filename like notify.php 然后使用诸如notify.php之类的文件名

This PHP function returns the real URL of a full path. 这个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;
}

Call of pathUrl in this file : http://example.com/shop/index.php 在此文件中调用pathUrl: http : //example.com/shop/index.php

#index.php

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

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

#index.php

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

For sub directory : http://example.com/alias-name/shop/inc/config.php 对于子目录: 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 https://stackoverflow.com/a/36101073/3626097

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM