PDA

View Full Version : Common Menu and Headers?



cpc
07-16-2007, 11:03 PM
Hi all,

What do most people do for common menus and header links? I'm creating a new site and find myself constantly changing the menu content as I learn more about SEO (thanks to this site).

Right now, my site is straight HTML with CSS (original template from oswd). My understanding is that is best to have your menus on each page but this it is fairly tedious to update all my pages for each tweak. Of course, I probably should have just designed the menus first. Still, I think I may be missing something obvious. Thanks.

--CPC

Nico
07-16-2007, 11:34 PM
What's usually done is to put the Menu (or Footer or Header) in one place and include it in all the pages. That way, when you want to make a change, you only have to change it in one place.

A very common way of doing this is using PHP and including your Menu file into all the pages on your site. So you can put your menu in a file named "menu.inc.php" and then when you want to show your menu in a page, you just include it like this: <?php include 'menu.inc.php'; ?>

But of course all your pages must use the .php extension in order to do it that way. You have other ways of doing it without PHP, but this is one of the better ones in my opinion.

Chris
07-17-2007, 05:40 AM
The other way is with SSI, server side includes. Searching Google should find you an amiable tutorial.

Dan Schulz
07-17-2007, 01:43 PM
Or read this post. :) I posted this last year on Digital Point's forums (I really need to centralize everything). I'm going to provide the link to the post at the end of this one.


For server-side includes, (after creating your separate file), put this into your Web page:


<!--#include virtual="/path/to/filename.ext"-->

where /path/to/filename.ext is the path to your included file (such as menu.html) relative to the document root (the folder where your Web pages go, usually public_html or www) and .ext is the extension of the file (.html for example). I personally prefer to put all my include files into a single folder, directly below the root (again, usually public_html or www), called includes.

For example, the path would be /includes/menu.html

You would then save the Web page as a .shtml file (or configure Apache to parse .html as .shtml files so you can just keep the .html extension, but I'm not comfortable enough with Apache to do this).

As for PHP, create your included file as normal, then follow the same instructions above, but use this code instead:


<?php include("filename.inc.php"); ?>

Of course, if you're using multiple folders, you'll have a problem referencing your includes, even if you put them into a separate includes folder.

For that, I usually do this (even though I should know better):


<?php include($DOCUMENT_ROOT . "/includes/filename.inc.php"); ?>

Notice how in each case I declared the file extension to be .inc.php ? That's because the .inc is to remind me that it's an include page, and the .php is to force the server to parse it before sending it to the browser. If you have any sensitive data (like database login information) this becomes critical, since any schmuck could go to yoursite.com/includes/filename.inc (let's just say that filename.inc is database.inc for a second) and get the database login information if you don't put .php at the very end.

And of course, rather than saving your Web page (with the include statements) as .html, you would save them as .php unless you configured the server to treat .html files the same as .php files.

Hope that helps. If you need more, just ask.

[Original Post: One Menu For All Pages (http://forums.digitalpoint.com/showthread.php?p=2145250#post2145250)]

Something I forgot to mention in the original post. If you're using PHP, and you're calling sensitive data, like database queries, then put your includes folder outside of your root so that it's inaccessible to anyone who may be snooping around looking for an easy way to compromise your site's security. You'll have to modify the path to do this though.

cpc
07-17-2007, 08:24 PM
Great, thanks for the responses. I can setup a local apache to try SSI and will probably also try PHP (after I learn it :)

cpc
07-17-2007, 10:57 PM
One quick followup question. In PHP, is there a nice way generate the dynamic absolute domain for all your links?

For example, if my site is www.foo.com, I prefer to use absolute paths (http://www.foo.com/somepage.php) instead of relative paths. I know I can use the $_SERVER['HTTP_HOST'] to prepend but that is very clunky.

Or do most of you just hardcode your domain in and edit your hosts file to point to you local development server?

Thanks!

Dan Schulz
07-18-2007, 01:47 PM
I just develop locally using relative link paths and the <base> element in the HEAD section of my Web pages on my local server, then remove the BASE element from the code when moving it over to the server (no need to change the link paths) since they'll point to the Web root anyway).