include() Across Different Directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Toot4fun
Forum Commoner
Posts: 25
Joined: Wed Dec 10, 2003 11:44 am

include() Across Different Directories

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

lose the .. and just have /file.ext
Toot4fun
Forum Commoner
Posts: 25
Joined: Wed Dec 10, 2003 11:44 am

Post 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)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Look in the manual what the 'include_path' setting can do for you...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply