1

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.

Kuntal Basu
  • 830
  • 2
  • 12
  • 28

3 Answers3

4

$_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.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • I have tried this http://'.$_SERVER['HTTP_HOST'].'/store_esp/notify.php?status=T But its giving http://localhost/store_esp/notify.php?status=F But I need http://localhost/website/store_esp/notify.php?status=F – Kuntal Basu Dec 18 '10 at 07:21
  • . Your previous answer was right.. I need to use $_SERVER['HTTP_HOST']. When it will be on live it would be ok. So please revert your answer. Actually 2nd 1 is like doing anything force fully. Thanks for the answer. – Kuntal Basu Dec 18 '10 at 07:52
4

use this

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

and after this use the filename like notify.php

Bhanu Prakash Pandey
  • 3,805
  • 1
  • 24
  • 16
0

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

Community
  • 1
  • 1
fallinov
  • 181
  • 1
  • 5