homepage Welcome to WebmasterWorld Guest from 54.235.227.60
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php absolute paths for includes, headers, and echos
clarification on dirname() and $_SERVER['DOCUMENT_ROOT']
t1000mills




msg:4479988
 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");

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!

 

omoutop




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

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.

t1000mills




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

@omoutop

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.

omoutop




msg:4480051
 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.

g1smd




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

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.

t1000mills




msg:4480903
 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved