
Sometimes you need an absolute URL, when redirecting with a header('Location: …') for example. Or sometimes you would like a relative URLs to, say your style sheet, but you do not know precisely where your template is being displayed on the site.
If you have a fixed point you can work out the answers to this, for example the site root - /site for this site. Sometimes this can be calculated with a combination of $_SERVER[] and __FILE__, or sometimes you could use a config file, usually it's hard coded.
If none of these approaches suits your site you can use "Where Am I". Once called for the first time from the index page it will make a permanent record of where it and the site root are. Thereafter all other pages can use "Where Am I" to generate relative or absolute URLs.
Browse the source or check it out:
svn checkout http://svn.tepic.co.uk/svn/tools/php-where-am-i
// index.php require_once "whereami.php"; $wai = new WhereAmI($_SERVER["PHP_SELF"]);
// somewhere/foo/page.php $wai = new WhereAmI(); header("Location: " . $wai->url("/index.php"));
