joined:July 30, 2012
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:
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';
$file = dirname(__FILE__).'/index.php';
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
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!