简体   繁体   中英

Php site root url

I am doing a payment by paypal using submit the item details to paypal. Here I need to specify a link of my 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

and my url for notify.php should be http://domainname/store_esp/notify.php

Presently my domail name is http://localhost/website/

How can I get the domainname dynamically using php.

$_SERVER['HTTP_HOST'] will give the domain name

 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.

use this

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

and after this use the filename like notify.php

This PHP function returns the real URL of a full path.

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

#index.php

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

Work with alias : 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

#config.php

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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