tags:

views:

278

answers:

8

Hi,

I have a website which consists of a bunch of static HTML pages. Obviously there's a lot of duplication among these (header, menu, etc). The hosting company I plan to use supports PHP, which I know nothing about. Presumably PHP provides some sort of #include mechanism, but what changes to I need to make to my HTML pages to use it?

For example, suppose I have a page like this

index.html

<html>
  <head></head>
  <body>
    <h1>My Common Header</h1>
  </body>
</html>

Obviously I need to move the common part into it's own file:

header.html

<h1>My Common Header</h1>

Given the example above (and assuming all files are in the same directory):

  • What do I add within the body tag to get header.html included?
  • Do I need to rename index.html or add some special tags to indicate that it's a .php file?
  • Do I need to make any changes to header.html?

Update: I want to emphasise that my objective here is simply to find the lowest-friction means of reducing duplication among static HTML files. I'm a bit reluctant to go down the server side includes route because I don't yet know what type of server (IIS/Apache) I'll be hosting the files on, and whether includes will be turned on or off. I was drawn towards PHP only because it is about the only thing I can presume will be available that will be able to do the job. Thanks for the responses.

Thanks, Donal

+6  A: 

You don't need to use PHP to get this functionality, and it's generally a bad idea to do so due to potential security concerns. Essentially, you're swatting a gnat with a nuclear bomb. If you're not using a dynamic language, then you're looking for server side includes.

In IIS, for instance:

<!--#include virtual="file.inc"-->

Be aware that you often have to configure the server to utilize them, as this feature is often turned off by default. Both IIS and Apache support server side includes, but they use different configurations.

You can find more information here:

Server Side Includes

EDIT: I don't mean that it's a bad idea to use PHP, just using PHP solely for including other files. It creates a larger attack surface by bringing PHP into the mix when it's not needed, thus the potential for security issues when the functionality of PHP is not required.

EDIT2: I think it's a bad idea to assume you won't be a target because of your size, and thus you can ignore security. Most sites are compromised by automated worms and turned into malware hosts, spam zombies, or pirated software/media servers. Apart from the fact that you might end up being involved with infecting others, your site can become blacklisted and it can cost you real money in bandwidth overage charges. We're talking hundreds or thousands of dollars.

Just because you're a small site doesn't make you any less of a target. Just being on the internet makes you a target.

Mystere Man
Agreed; it's not necessary to use PHP to do inclusion unless you're planning to incorporate PHP into some of the included files at a later date.
Rob
The gnat/nuclear bomb analogy works, but since I wrote my long answer...
Wayne Khan
I've updated my post to indicate why I'm reluctant to use server side includes. The website in question is the exact opposite of mission critical, will attract *very* little traffice, so I really couldn't care less about the size of my attack surface.
Don
everyone needs to worry about their attack surface in this day of zombies and worms. You don't become a target because you're big, you become a target because you're a site on the internet, that's it.
Mystere Man
Don - if you aren't going to follow best practices you are in the wrong industry.
Syntax
Thanks Syntax, helpful *and* profound. Take a bow.
Don
+6  A: 

You are looking for include (or one of its derivative such as include_once, require, require_once):

header.php

<h1>My Common Header</h1>

index.php

<html>
  <head></head>
  <body>
    <?php include('header.php'); ?>
  </body>
</html>

And so on, for your footer for example.

lpfavreau
+2  A: 

You should first change the file extensions of index and header to be .php, then you can do:

<html>
<head></head>
    <body>
        <? include 'header.php'; ?>
    </body>
</html>

And your header.php file just has

<h1>My Common Header</h1>
Steerpike
You are better off using the full <?php tag instead of the <? tag as the short tag version is not supported by default anymore in PHP5 as it can conflict with other languages.
lpfavreau
+1  A: 

While you can just use the "include", "require", or "require_once" directives to include things in one page, you might have better luck with a template engine like Smarty

Kevin
A: 

OK this is a semi-programming related question only.

PHP does have include(), which is really easy to use, but it doesn't contribute to future maintainability. I wouldn't recommend it, especially for big sites.

I'm a pro-frameworks. I've used CodeIgniter, CakePHP and even Smarty template engine. If you are serious about PHP, do consider CakePHP. There's this "layouts" concept where you frame your header, footer, css, javascript outside of the main content; e.g. for the "about us" page, your content would be something like:

This is an about us page that tells you a whole bunch of stuff about us...

CakePHP takes this this content, and wraps your layout around it:

header
css
javascript
This is an about us page that tells you a whole bunch of stuff about us...
footer

Wayne Khan
You are absolutely right about the advantages of using a framework for website development but I'd like to point out that the OP did not talk about getting serious with PHP nor does he seems to require more than an include as he's running with a static website at the moment.
lpfavreau
And an include does contribute to future maintainability, even though not as much as other solutions: frameworks, CMS, etc. It's definitely easier to change the content of menu.php to change your menu than go through 30 static pages.
lpfavreau
I hestitate to step outside my comfort zone (PHP) into things like server-side includes. Otherwise I think Mystere Man's answer is probably most useful.
Wayne Khan
OK, maintainability is the wrong word. I'm trying to find the word, but I can't. I've simply spent too much time looking at crappily-coded PHP websites with include() and require() EVERYWHERE.
Wayne Khan
I think you mean understandable. It's hard to understand a page that is littered with includes.
Mystere Man
Ok, I see what you mean.
lpfavreau
understandable: great word! :)
Wayne Khan
I am definitely NOT serious about PHP, all I'm looking for is the lowest friction way to avoid duplication among a bunch of static HTML files
Don
Then go with the PHP includes. It might be good enough for your website, and it's really more a matter on how you organize your includes that will make it understandable. And anyway, in the frameworks, all the render_views are mostly glorified includes with output buffering. ;)
lpfavreau
See line 659 and 662 as an example of the glorified includes in frameworks: https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/libs/view/view.php. Don't get me wrong, I use frameworks and love them, just don't be too quick to bash them for someone who wants to quickly improve its static site.
lpfavreau
Thanks for the tip on Cake. At the very least there is a consistent way of including external content. It's the include() litter that frustrates me.
Wayne Khan
+1  A: 

While using an include file for the header is a solution I went a different route when I faced the problem several years back: I wanted all pages to use the same layout (which I assume is rather common ;-). Thus, as I only wanted to change the content of the page I made the page content the file that gets included and have a master template file that includes header and footer. For setting the page to be included I resorted to creating quite small php scripts that only set a variable that holds the page to get included. In some cases the page can also get named by a GET parameter. Of course this requires proper validation of that parameter. In the long run I don't need to worry about the HTML itself anymore -- all I do is write small snippets (which should be complete for themselves of course) that get included.

A possibly even better solution would be to use an existing template framework. Due to the contraints I had back then I wasn't able to do so, but I would do it when facing the same issue again.

bluebrother
+3  A: 

Forget doing it on the server altogether.

If all you really want to do is maintain some static pages -- and don't anticipate ever having to really use PHP -- I'd just do it with Dreamweaver, which will allow you create and manage templates and variable content on your end.

No includes needed. No templating engine needed. (These would be overkill for what you are trying to accomplish.)

Clayton
This is a pretty valid solution and requires no knowledge of PHP.
Syntax
I does require Dreamweaver however :)
Don
True, but sometimes it's best to use a hammer when you're driving nails. You may already have the wrench -- or be able to get a wrench for free -- but it's a pain to drive nails with a wrench. :)
Clayton
+1  A: 

Back in the day, I used SSIs (the "<!--#include virtual="file.inc"-->" method described above by Mystere Man) quite a bit for static HTML pages and I would definitely recommend using that.

However if you want to eliminate any uncertainty about whether support for that will be enabled on the server, you could develop your separate files locally and merge them into the resulting files before uploading to your server. Dreamweaver, for example, supports doing this in a seamless fashion.

Or you could do it yourself with a rather simple script in your language of choice by doing simple string replacement on markers in the files, replacing {{{include-header}}} with the contents of a "header.html" file and so on.

Edit

Oops! Somehow I didn't see Clayton's post with the same note about Dreamweaver.

Trevor Bramble