Let's say I have the majority of my site in the 'root' WWW directory on the server, such as:

www.domain.com/index.php

My current header & footer files are set to respect this configuration and work beautifully. I am now in the process however of creating a new directory which will be used in the main site, such as:

www.domain.com/newfolder/index.php

What would be the easiest way to make the header and footer file work in both directories instead of having to create a new header/footer file for each directory? Perhaps another way to say this, is there a way to append a "relative" file name depending on which directory the file is in?

Thanks!

Dani AI

Generated

Short answer: pick one canonical site root (server filesystem and/or site URL) and use it everywhere. That avoids keeping different header/footer copies or needing per-folder hacks. ’s DOCUMENT_ROOT idea works well for server-side includes, and ’s _path trick is a practical fallback — below are cleaner, more maintainable alternatives.

Create one config file in the site root that defines both the filesystem root and the base URL. Include that config from every script (or set it as an auto_prepend_file). Example config (place in your site root):

<?php
// config.php (site root)
define('BASE_DIR', __DIR__);               // filesystem root of the site
define('BASE_URL', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http')
                 . '://' . $_SERVER['HTTP_HOST']);

Then use BASE_DIR for server includes and BASE_URL for HTML links so paths are stable regardless of folder depth:

<?php require_once BASE_DIR . '/includes/header.php'; ?>

<li><a href="<?php echo rtrim(BASE_URL, '/'); ?>/page1.php">Page 1</a></li>

If you can’t edit every page’s HTML, another option is a single <base> element (inserted from the root header) so plain relative links resolve from the site base. Caveat: <base> affects all relative URLs (CSS, scripts, images), so adjust asset paths accordingly.

If you must keep a single menu.php and make it compute relative prefixes at runtime (useful on shared hosts), have it calculate how many directory segments the current script lives under and build a ../ prefix dynamically:

<?php
$segments = array_filter(explode('/', dirname($_SERVER['SCRIPT_NAME'])));
$prefix = str_repeat('../', count($segments));
echo '<a href="'.$prefix.'page1.php">Page 1</a>';
?>

Notes: use filesystem roots for includes, URL roots for anchors; watch for sites hosted in a subfolder (use BASE_URL there); and prefer one config approach for long-term maintainability.

Recommended Answers

All 4 Replies

Member Avatar for Member #120589
include('includes/header.php');

for top directory

include('../includes/header.php');

for second level directory

Is that it?

Wow talk about a quick reply! ;)

I am currently doing that but that's not really what I'm after here. Let me give another example that's not related to the header/footer above. Let's say I have a central file called menu.php that has a list of all of my pages found in the menu. If you were in the 'root' directory, the relative link would look something like:

page1.php
page2.php
page3.php

Usuing the same menu.php file though, when you are navigating the site and go to the 'test' directory, the links would break since they now refer to objects that exist in the 'test' directory, not the 'root'. So I am looking for something that does this:

If ( Directory = something other than root ) {
echo '../page1.php';
echo '../page2.php';
echo '../page3.php';
}

Else {
echo 'page1.php';
echo 'page2.php';
echo 'page3.php';
}

I realize that's not proper syntax, but just to give you an idea of what I am looking for. My problem is finding the variable/function in PHP that would find out what the directory the page currently resides in?

Member Avatar for Member #120589

There's a better way to do this, but a simple workaround would be:

include($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");

That'll work from anywhere in your code (most likely).


If it's for navigation, use absolutes:

<li><a href="/index.php">home</a></li>
<li><a href="/folder/newpage.php">page</a></li>
commented: This is the exactly what I needed! Thanks a lot! +0

or if using multiple directory structures

<?php define('_path','../../');//example only it would be different for each folder
include(_path.'menu.php');?>

and each item in the menu file also references the path <a href='<?php echo _path; ?>/downto/thisfolder.htm'>thisfolder</a>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.