SyntaxHighlighter

Tuesday, October 28, 2008

PHP Web Application Root URL

I wrote this little function to return a PHP applicaiton's root url. It is very quick and dirty, but I thought I'd still share it:
function get_app_root_url()
{
$app_root_dir_name = getcwd();
$idx = strripos($app_root_dir_name, '/');

if($idx >= 0)
$app_root_dir_name = substr($app_root_dir_name, $idx + 1);

$app_root_url = get_page_url();

$idx = strpos($app_root_url, $app_root_dir_name);

if($idx >= 0)
$app_root_url = substr($app_root_url, 0, $idx + strlen($app_root_dir_name));

return $app_root_url;
}

// Curtosy of http://www.webcheatsheet.com/PHP/get_current_page_url.php
function get_page_url()
{
$page_url = 'http';

if ($_SERVER["HTTPS"] == "on")
$page_url .= "s";

$page_url .= "://";

if ($_SERVER["SERVER_PORT"] != "80")
$page_url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$page_url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

return $page_url;
}

No comments: