Msg#: 4479986 posted 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
and $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: http://domain.com/kunden/homepages/7/d486255762/htdocs/domain/index.php when it should just be http://domain.com/index.php
When I try the second one I get a 404 and the url http://domain.com/homepages/7/d486255762/htdocs/domain/php/index.php 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!
Msg#: 4479986 posted 11:06 am on Jul 30, 2012 (gmt 0)
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.