Using External HTML Files

jburnett

New Member
Messages
2
Reaction score
0
Points
0
Okay, I'm aware of how to use external stylesheets so you can edit all the CSS from one page, but I want to know if it's possible to do it with an HTML document as well.

For example, I'd like to have the header and navigation menu at the top of my page in a separate file so I only need to edit one file, like I can with the CSS, rather than go through dozens just to add one new item to the navigation or change a broken link, etc.

I haven't had any success in finding an answer so far, so any help would be much appreciated.
 

Scott B

New Member
Messages
844
Reaction score
0
Points
0
Yeah, there's the PHP include (preferred):
Code:
<?php
include("navigation.php");
?>
or you could always use Iframes.
Hope I helped :)
*cough*rep me please*cough* :p
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
I'll explain it a bit more then just giving some code and hope he gets it :p

There's no way to use html code to insert pages in other pages, but it's possible to do it serverside, using PHP. (If you don't know what php is, try googling for it.)

Say you've got the page "index.php" (note the .php, php code is not executed in .htm or .html etc files!) with this (pseudo-) code:
HTML:
<html>
<head>
<!-- head stuff -->
</head>
<body>
<!-- a header, menu etc... -->
<!-- actual content -->
<!-- footer -->
</body>
</html>

As you can see, the only thing that would change on the different pages of your site is <!-- actual content --> (and probably the head-stuff).
The header and footer of the site are always the same. You could now put those in every page of your site, and loose a lot of time each time you even just rename a file... or you could use php.
Say we make 2 new files, one called "header.php" and one called "footer.php".
header.php:
HTML:
<!-- a header, menu etc... -->
footer.php:
HTML:
<!-- footer -->

Now we can change "index.php" to:
PHP:
<html>
<head>
<!-- head stuff -->
</head>
<body>
<?php include "header.php"; ?>
<!-- actual content -->
<?php include "footer.php"; ?>
</body>
</html>

This code will probably not work on your pc (unless you have a server with php installed?) but once you upload this, it will output the same as the origional index file you wanted to achieve.

Note: the function "include" will attempt to include the specified file, but if it fails, it outputs an error message and continues. If you want it to not output an error and continue, put an @ in front of it ("@include"). If you'ld want it to stop if it fails, use "require" ("@require" works too)


- Marshian
 

jburnett

New Member
Messages
2
Reaction score
0
Points
0
I tried to use the code:
PHP:
<?php include "header.php"; ?>

But I kept getting error messages. After some research on the error message from our friend Google, I found an alternate way to do it, using:

PHP:
<?php $file_contents = file_get_contents('header.php');

echo $file_contents;?>
And I got it working. :biggrin:

Now I can do regular maintenance on the site a lot faster, especially when I want to change any global settings. Thanks a lot for the help!
 
Messages
740
Reaction score
1
Points
18
I tried to use the code:
PHP:
<?php include "header.php"; ?>

But I kept getting error messages.

That's because you're missing some brackets.

It should look like this:
PHP:
<?php include("header.php"); ?>
Note the " ( " and " ) " before and after the quotation marks and before the semi colon.

I'd recommend you change it to the php include - I'm not sure what the other piece of code you're using actually does even though it ends with what you're wanting this way is a little more secure as that can open up un-needed and unwanted things.


Please add reputation if you thought this was helpful.

Thanks

Martin
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Using include without the parentheses is actually the preferred way since adding them forces the processor to evaluate the enclosed content before including. Albeit the overhead is minimal if not negligible, but I just wanted to point out that parentheses aren't needed.

The current method you're using with file_get_contents() is basically the same as just using readfile('header.php');. However, both of these methods output all of the file's contents -- even the php. So if you have any php code in the file, include or require should be used instead.
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
The current method you're using with file_get_contents() is basically the same as just using readfile('header.php');. However, both of these methods output all of the file's contents -- even the php. So if you have any php code in the file, include or require should be used instead.

Or: the php in that file gets executed first, then only the result is brought into your page, so if you want the 2 pages to be able to exchange variables etc., you should still use include/require.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
What do you mean? There is no 'or'. file_get_contents() reads all the contents of the file, it doesn't sometimes decide to execute the php :p
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
What do you mean? There is no 'or'. file_get_contents() reads all the contents of the file, it doesn't sometimes decide to execute the php :p

I mean 'or' as in "I don't think you've made yourself clear enough to a person who has absolutely no idea how these things work, so I'll try to explain it a little better."

file_gets_contents just requests the file, just like the user's browser would do, so php gets executed before the file is incluced, while include just includes the file, then it executes php. (=> just use include, don't ask questions)
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Ah I see what you meant. But I'm not sure what you mean by the php is executed before the file is included. file_get_contents() reads *all* of the contents into a string. It's just like calling fopen(), fread(), fclose().

The only way the php would be executed is if you request the script through http, as you would with files on remote servers(or on your own server if you wanted to for some reason). E.g., file_get_contents('http://www.site.com/header.php');

Regardless, jburnett, if you don't have any php code in header.php then stick with what you're using. But if you do, use include.
 

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
I think the PHP include is the best way to go. I have used it many times and it saves loads of time especially when you need to change the navigation bar; who wants to change the navigation bar on every page?
 
Top