1

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?

Stefan
  • 1,051
  • 3
  • 11
  • 23
  • possible duplicate of [How can I find an application's base url?](http://stackoverflow.com/questions/176712/how-can-i-find-an-applications-base-url) – alexn Jan 08 '11 at 16:36
  • Thanks, but I just tried the solution there and the result it gives depends on the location of the file including the given code. – Stefan Jan 08 '11 at 16:44
  • Or rather: The solution requires the bootstrap.php to always be in the root directory (altough I said otherwise for simplicity's sake, I had it in an "includes/" directory). But I guess the simplest solution is to move it to the top and use the here-linked solution. – Stefan Jan 08 '11 at 16:56

1 Answers1

4

Anything involving realpath() and DOCUMENT_ROOT is going to fail hard when the server's got aliases configured. Consider a scenario where Apache's got a configuration like this:

DocumentRoot /home/httpd/html
Alias /testalias /home/otherdir

And you access a script at example.com/testalias/script.php.

The script will return:

realpath(dirname(__FILE__)) -> /home/otherdir
$_SERVER['DOCUMENT_ROOT'] -> /home/httpd/html
BASE_DIR -> /home/otherdir
BASE_URL -> /home/otherdir/

and yet the rest of the site actually exists in /home/httpd/html

You might have better luck reconstructing the URL based on $_SERVER['SCRIPT_NAME'], which is the path/script name portion of the URL:

$_SERVER['SCRIPT_NAME'] -> /testalias/script.php
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Which is what the solution does which was linked by alexn as a possible duplicate. And I think he is right, the question is the same, but I still think the solution there only works in a bootstrap file in the application root. http://stackoverflow.com/questions/176712/how-can-i-find-an-applications-base-url/185725#185725 – Stefan Jan 09 '11 at 00:32
  • I am using this when I am using Alias directory str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']) – kiranvj Jun 29 '13 at 11:02
  • The problem with SCRIPT_NAME is that it is not static because it is based on the name of current executing script. Before Apache 2.3.x becomes popularly adopted to that we can use CONTEXT_PREFIX variable. I'm afraid there's no solution. – devXen Oct 10 '14 at 19:25