7

I currently have the following in a config file in my application:

define('DOCROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);
define('WEBROOT', 'http://localhost/samples/');

The first line works perfectly. I can include the config file anywhere and then use the DOCROOT constant as expected.

The second line works as well, but it's hardcoded, which means that when I upload this file to my webserver, it will be wrong. There it should be http://samples.example.com. Is there a good way to somehow prevent this hard coding? I kind of think that I have to hard code something somewhere, and in that case, what and how little can I get away with?

Svish
  • 138,188
  • 158
  • 423
  • 589
  • I am interested in this as well. I have solved this problem in the interim by using `$_SERVER['SERVER_NAME']`. However, I've read that not all servers support the same `$_SERVER` options so I'm not sure how portable this would be – Charles Sprayberry Jul 04 '11 at 17:03
  • 1
    There isn't really anything wrong with $_SERVER['SERVER_NAME'], it is one of the few options really available unless you want have the information manually written into a file or database that is then returned as a variable. The other option is to re-write much of your DOM applications into another language such as ASP.NET or JavaScript that has these variables available on practically all current installations. The problem in writing just this object in JS is simply the PHP will parse before the JS so this is not viable. Well that's my take on the situation anyway after writing a few web apps – Ryan Jul 04 '11 at 17:22
  • Check my answer http://stackoverflow.com/questions/6574248/php-how-not-to-hard-code-web-application-root/6574452#6574452 – SAIF Jul 04 '11 at 17:38

5 Answers5

3

I use the following code to find the base URL:

function getBaseURL() {
    return 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://'
         . $_SERVER['HTTP_HOST']
         . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\')
         . '/'
    ;
}
NikiC
  • 95,987
  • 31
  • 182
  • 219
2
define('WEBROOT', 'http://'.$_SERVER['HTTP_HOST'].'/');
Mfoo
  • 3,105
  • 1
  • 13
  • 11
  • 1
    That would be the path to the current running script or something, which is what DOCROOT gives you. Except I what I use will give me the directory of the directory where the config file is, and not whatever script is running. – Svish Jul 04 '11 at 17:06
  • Do you know of any difference between `$_SERVER['HTTP_HOST']` and `$_SERVER['SERVER_NAME']`? They appear to be returning the same value on my test machine. – Charles Sprayberry Jul 04 '11 at 17:07
  • That would be wrong in the case of my local version, which with your code would just become `http://localhost/`. It should have been `http://localhost/samples/`. – Svish Jul 04 '11 at 17:12
  • @Charles: Could perhaps use that for redundancy? If one isn't set, try the other? – Svish Jul 04 '11 at 17:13
  • @Svish, I suppose. You could still use this method. Depending on where you define the code you could do something like `basename(dirname(__DIR__))`. If setup properly will give you the name of the install directory. You could then mush this onto the end of the `define`. – Charles Sprayberry Jul 04 '11 at 17:16
  • HTTP_HOST is the content of the HTTP/1.1 HOST header. SERVER_NAME in a multiple virtual domains configuration could resolve to the canonical name for the server, which in most traumatic configurations is your provider's one. – ZJR Jul 04 '11 at 23:55
1

You can go one step further, and ensure if the user is accessing via HTTPS, you give them HTTPS....

    if (isset($_SERVER["HTTPS"]) && $_SERVER['HTTPS'] == 'on') {
            $target_url = "https://" . $_SERVER["SERVER_NAME"];
    } else {
            $target_url = "http://" . $_SERVER["SERVER_NAME"];
    }
   define('WEBROOT', $target_url);
sdolgy
  • 6,601
  • 3
  • 37
  • 60
0

One way to "hack" this is to check for the external requesting IP. If the ip is 127.0.0.1 define WEBROOT as http://localhost/samples, otherwise define it as http://samples.example.com

gnur
  • 4,512
  • 1
  • 20
  • 31
0
'http://'.$_SERVER['HTTP_HOST']. str_replace(str_replace("\\","/",str_replace(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR, "", realpath($_SERVER['SCRIPT_FILENAME']))), "", $_SERVER['PHP_SELF']);

I tested it on localhost. Put it in your config file.

SAIF
  • 181
  • 1
  • 6