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.
include() Across Different Directories
Moderator: General Moderators
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)
(http://www.mysite.com/admin looking for header.php in http://www.mysite.com)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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.
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.No good - it doesn't include my file at all. I don't get an error either.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You could always specify a file path in your header file and declare it in your scripts.
This way, regardless of which page you are in the reference to the location of the file is retained throughout the script tree structure.
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>';
?>