I develop and deploy various PHP applications to different environments. Especially on development environments, they can be anywhere, from document_root to /Users/me/Sites/ or even /Users/me/Sites/someapp/
Inside these applications I need to know where the 'application root' is, once as the real path and once as URL. Path is no problem. Let's say I have a bootstrap.php in the app root directory which does:
define("BASE_DIR", realpath(dirname(__FILE__)));
However, I have problems to reliably get the base URL. On most environments simply subtracting document root from BASE_DIR works:
define("BASE_URL", str_replace($_SERVER['DOCUMENT_ROOT'],'',BASE_DIR) . "/");
Now, my problem is: This does not work on environments where my app lies inside my user directory because PHP still sees the main document root. Has anyone solved this problem?