I use the following in a homebrew framework... Put this in a file in the root folder of your application and simply include it.
define('ABSPATH', str_replace('\', '/', dirname(__FILE__)) . '/');
$tempPath1 = explode('/', str_replace('\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\', '/', dirname($_SERVER['PHP_SELF'])));
for ($i = count($tempPath2); $i < count($tempPath1); $i++)
array_pop ($tempPath3);
$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);
if ($urladdr{strlen($urladdr) - 1}== '/')
define('URLADDR', 'http://' . $urladdr);
else
define('URLADDR', 'http://' . $urladdr . '/');
unset($tempPath1, $tempPath2, $tempPath3, $urladdr);
The above code defines two constants. ABSPATH contains the absolute path to the root of the application (local file system) while URLADDR contains the fully qualified URL of the application. It does work in mod_rewrite situations.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…