Page 1 of 1

include() Across Different Directories

Posted: Thu Apr 20, 2006 9:47 am
by Toot4fun
I have a folder called /www that contains the pages for http://www.mysite.com. Within that /www folder is a header.php, which is my standard header for all my pages. Pretty much all pages within the /www folder have an include("header.php") line in them at some point. header.php is pretty basic, mostly just images and links, nothing too fancy.

Within the /www folder is another folder called /admin. If I try to include the header.php file in a page within /www/admin, the relative links are incorrect, causing me to get dead links and missing images. If I change the links and images in the header.php file to include ../, this works for my /www/admin problem, but if I want a /www/admin/blah, it's broken again.

Is there a better way to accomplish this without having to hard code the full URL in my header.php (http://www.mysite.com/style.css rather than style.css or ../style.css for example)? I would also like to avoid having multiple header.php files, but I guess this is always an option.

Posted: Thu Apr 20, 2006 11:29 am
by feyd
lose the .. and just have /file.ext

Posted: Thu Apr 20, 2006 12:48 pm
by Toot4fun
No good - it doesn't include my file at all. I don't get an error either.

(http://www.mysite.com/admin looking for header.php in http://www.mysite.com)

Posted: Thu Apr 20, 2006 1:01 pm
by timvw
Look in the manual what the 'include_path' setting can do for you...

Posted: Thu Apr 20, 2006 2:00 pm
by Ollie Saunders
are you certain you should be using the slash at the start? you realise this will reference the root of the filesystem and unless you are localhosting and have configured it this way this is unlikely to be the place where the web server serves from. I'm assuming that the reason this directory is called www is because html/php is served out of it.
No good - it doesn't include my file at all. I don't get an error either.
use require_once instead of include, then you are more likely to get an error when it fails or at least the parsing will halt.

Posted: Thu Apr 20, 2006 4:37 pm
by RobertGonzalez
You could always specify a file path in your header file and declare it in your scripts.

Code: Select all

<?php
// This is in a file in your root folder
$page_path = './';
?>

Code: Select all

<?php
// This is in a file in your sub-folders
$page_path = './../';
?>

Code: Select all

<?php
// This is header.php
echo '<a href="' . $page_path . 'file.php">This is a link</a>';
?>
This way, regardless of which page you are in the reference to the location of the file is retained throughout the script tree structure.