I want to include a .php file, containing a menu bar, in all my pages. Thing is, some of these pages are at different levels: for example, on might be in the root directory, another might be in root/news, another in root/news/2012, and so on. What can I do to make this include always link back to the menu bar file, which is located in my root directory?

You could do something like

include('path/to/file/header.php'); //path/to/file being full path name 

But to make a link always point to a file in the root folder, don't I have to add something along the lines of ../menubar.php or ./menubar.php?

no you can use

include('http//yourdomain.com/header.php');

I see...
I'm new to PHP. In your second example, is "DOCUMENT_ROOT"supposed to be replaced by the path to the root, or does it just state that the next path is root-relative?

ive removed that for now. Just try the first one and see how you get on.

I get this:

Warning: include() [function.include]: URL file-access is disabled in the server configuration

ok you need to set the absolute path now this depends how you webserv is setup.
add this to you header.php (menu bar file)

echo $_SERVER['DOCUMENT_ROOT'];

this will give the full path to header.php file then use this instead of http://www.yourdomain.com

so it should look something like

include('var\www\header.php');

That seems to work perfectly; however, just before trying this, I tried ./menubar.php, which also worked; although, that's probably not an absolute path. Thanks!

Yes using ./ will work but if you go deeper it started to get messy. eg ../../../../header.php

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.