Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php absolute paths for includes, headers, and echos

clarification on dirname() and $_SERVER['DOCUMENT_ROOT']



2:29 am on Jul 30, 2012 (gmt 0)

I am building my first php site. I am running into trouble when trying to access files from different directories. for example if I had 4 files: index.php, php/about.php, php/header.php, and php/login.php. If php/login.php is referenced within php/header.php and then i need to include php/header.php in both index.php and php/about.php from a template (without hand coding a new path for each directory), some things are not working correctly.

I know this question gets asked often and I have searched around and tried several things. I have php/header.php included as:
include $_SERVER['DOCUMENT_ROOT'].'/PHP/header.php';
and it works from both index.php and php/about.php so this does not seems to be a problem.
I run into a problem when I try to locate back to index.php from within php/header.php with the header function. I have tried

$file = $_SERVER['DOCUMENT_ROOT'].'/index.php';
header("Location: $file");


$file = dirname(__FILE__).'/index.php';
header("Location: $file");

When I try the first one I get a 404 file not found and the url is something like:
when it should just be

When I try the second one I get a 404 and the url
which i know isn't right because index.php isn't in the php/ directory.

I have a fix right now using something like

header("Location: http://domain.com/index.php")

but i'm sure this is not the best way to do it.

I'm also having a similar problem when I try to echo some html code that contains links.

I'm wondering if all this has something to do with the host server i'm using not giving the correct root directory since the url is so long?

I know I have given a lot of information here but I hope this helps somebody to help me. Thanks in advance!


8:35 am on Jul 30, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

welcome to webmasterworld.

For your html links, I would suggest using absolute paths:
<a href="/index.php">index</a> or <a href="/some/other/page.php">hello</a>

Your header("Location: [domain.com...] is perfect and legit way for php redirection.


8:46 am on Jul 30, 2012 (gmt 0)


thanks for your reply. my problem with the links is if i have them in a file that gets included in different directories, the link path will be incorrect for some of them.


11:06 am on Jul 30, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I assume you use $_SERVER variables or dirname() like you show on your examples. Don't to that. Simple absolute path can never be wrong.

<a href="/some/other/page.php">hello</a> this will never be wrong, unless there are no folders or pages with those names - no matter where the file resides in your website structure, this link will be correct.


8:43 pm on Jul 30, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

You need to be clear that filepaths within the server are not at all the same thing as URLs that are used out on the web.

When you "include" a file, this is done wholly within the server, so the reference needs to refer to the entire folder structure in order to work.

When you refer to a page as a URL as seen from the web you need to refer not to the whole server internal folder structure, but instead only to folders below the web document root.

Additionally, the location directive MUST include the protocol and hostname as well as the path. Without a status code, 302 is the default.

When referring to index pages as URLs do not include the index filename within the link. End the URL with a trailing slash.

Begin all links with a leading slash, i.e. root relative.


1:00 pm on Aug 1, 2012 (gmt 0)

Thanks, you've helped me a lot. Also I realized I was using dirname() wrong and that I need to repeat it for each path level.

Featured Threads

Hot Threads This Week

Hot Threads This Month