0

I use this $path in my Hostgator Shared Hosting accounts and it works perfectly:

$path = dirname($_SERVER["DOCUMENT_ROOT"]).'/';

I.e. this path is /home/username/public_html/

Here's an example of how I use it:

<?php 
$path = dirname($_SERVER["DOCUMENT_ROOT"]).'/';
include($path.'stats.php');
?>

My problem is that this $path doesn't work in my new Hostgator Reseller account.

Does anyone know why this doesn't work for Reseller hosting and what $path I can use instead?

Ideally I'd then use an "if else" so that I could use the same code on my Shared and Reseller accounts.

Would the following work:

<?php 
if(strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===false) {
        // for reseller accounts
        $path = WHATEVER_THE_RESELLER_PATH_IS;
    }
    else {
        // for shared hosting accounts
        $path = dirname($_SERVER["DOCUMENT_ROOT"]).'/';
    }
include($path.'stats.php');
?>

I got the above "if" from here: PHP strange DOCUMENT_ROOT.

Community
  • 1
  • 1

2 Answers2

0

dirname(__FILE__); will point to the directory of the file executing this statement. if you know where it is you can find your document root this way.

Basti
  • 3,998
  • 1
  • 18
  • 21
0

I would not build apps to be dependent on $_SERVER['DOCUMENT_ROOT'] always having a consistent relationship to your web root. Your application(s) should be loosely coupled to their hosting environment: They should know as little about it as possible, so that there are fewer issues moving between environments.

For example, WordPress uses the ABSPATH constant to store the absolute path to the WordPress directory:

The file: /example/hosting/env/public_html/blog/wp-config.php

would have the line of code:

define('ABSPATH', dirname(__FILE__) . '/');

WordPress knows where all of it's files are relative to the directory that holds wp-config.php, and so builds all of it's file paths relative to that. The next line of code down from that is:

require_once(ABSPATH . 'wp-settings.php');

After ABSPATH is defined, it is used as the base file-path for the rest of WordPress.

Shad
  • 15,134
  • 2
  • 22
  • 34
  • Hi Shad, thanks for this. Forgive me, I'm a beginner at this kind of thing. – Phil Dean Feb 21 '12 at 13:07
  • So, you're saying I should set my path to be `$path = dirname(__FILE__) . '/';` ? This doesn't work for Shared Hosting. Hence why I chose to use `$path = dirname($_SERVER["DOCUMENT_ROOT"]).'/';` which works fine on Shared hosting. Are you saying that I should use `$path = define('APP_ROOT', dirname(__FILE__) . '/');` and that will work, not just for shared hosting but for any hosting? Sorry, I'm confused. I use my code in hooks in my Headway framework theme (wordpress). – Phil Dean Feb 21 '12 at 13:19
  • @PhilDean Answer updated, let me know if that answers your questions – Shad Feb 21 '12 at 17:20