Homeowner Loans - Compare Credit cards - Loans - Mobile Phones - Homeowner Loans

PDA

View Full Version : how to insert header/footer server side for existing html files


jjdubb
Mar 23rd 2004, 11:34 pm
I have existing html files that I would like to insert headers and footers onto. There are too may to do by hand. I would like to do some kind of search and replace to add the the code in the correct place server side. What's the best way to do this? Preferably in php or perl. Thanks.

digitalpoint
Mar 23rd 2004, 11:40 pm
Do you want to insert the header/footer statically (so a copy of it exists within each file), or have the files include a "master" header/footer file?

- Shawn

Foxy
Mar 24th 2004, 12:27 am
I would like to know this too and is it possible to create a dynamic site map? :)

Mr T
Mar 24th 2004, 12:50 am
With PHP, you can just do include 'header.php'; Then put in header.php all the html you want to go at the start of each page.

Foxyweb, apparently is it possible to create a dynamic sitemap, but it really depends on your site as to how well it works.

Foxy
Mar 24th 2004, 2:39 am
Can you tell me more on the site map question?

For instance what sort of site does it work well on?

Thanks

Mr T
Mar 24th 2004, 4:23 am
Sorry, I dont really know much more, just read it on a forum somwhere.

I'm sure hotscripts.com could furnish you with some auto sitemap code, and maybe even a tutorial.

jjdubb
Mar 24th 2004, 8:23 am
I would like the header/footer to be inserted server side as the file is requested. I know I could manually add an include statement where needed in each file. I would prefer to have the code automatically inserted so that I don't really have to do anything else.

An example of what I want to accomplish is basically what the free hosting sites do with advertising headers/footers.

When you upload an html page to the free hosting site - they add an advertising header and footer server side without tampering with the rest of the page's html.

digitalpoint
Mar 24th 2004, 8:50 am
The best thing to do (that I can think of) would not be to alter each individual file, but use mod_rewrite to pass everything through a simple PHP script (or whatever scripting language you like) that does it for you on the fly.

- Shawn

jjdubb
Mar 24th 2004, 10:09 am
So, how would I setup a mod rewrite to insert a header/footer on every html file?

digitalpoint
Mar 24th 2004, 10:10 am
Well first things first... are you using an apache web server with mod_rewrite enabled?

- Shawn

jjdubb
Mar 24th 2004, 11:56 am
Yes, I have apache with mod rewrite available.

digitalpoint
Mar 24th 2004, 12:37 pm
You could have your .htaccess file setup like so in the root of the website:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !passthru.php
RewriteRule ^(.*) /passthru.php?file=$1
</IfModule>

Have passthru.php in the root of your web directory as well...

<?php
readfile("header.inc");
readfile($file);
readfile("footer.inc");
?>

header.inc and footer.inc also in the same folder (those of course are your header/footer files).

Then any request for a file (regardless of directory) within the site will have header.inc added to the beginning and footer.inc added to the end.

You could get trick within the passthru.php file and do a little better insertion (for example insert the header right after the <BODY> tag for example). But that should get you started...

Ultimately the best thing to do would be to make the footer.inc file link to www.digitalpoint.com though. hahaha

- Shawn

jjdubb
Mar 24th 2004, 1:31 pm
Thanks, that's exactly what I'm looking for. Do you know the php insert command right off the top of your head?

digitalpoint
Mar 24th 2004, 1:33 pm
What insert command?

- Shawn

jjdubb
Mar 24th 2004, 2:08 pm
The command to search for the a particular tag on the existing html file that is being passed through and then insert the the header code.

digitalpoint
Mar 24th 2004, 2:22 pm
Oh... there isn't a single command for that... you basically would need to read in the file, evaluate it and do some manipulation of the text instead of just using the readfile() command (which passes it right through).

jjdubb
Mar 24th 2004, 2:27 pm
Cool, I'm going to go to work on the script then. Thanks a lot!

nlopes
Apr 3rd 2004, 7:06 am
Hey guys.
Stop! The digital point script is a bit dangerous!! You should make sanity checks before opening the file or your passwords......

http://host.com/passthru.php?file=../../../../etc/xxx

PHP has a directive to append files automatically. Check your php.ini file:
"
; Automatically add files before or after any PHP document.
auto_prepend_file =
auto_append_file =
"

you may also set this in a .htaccess file.

digitalpoint
Apr 3rd 2004, 9:18 am
Wouldn't work of course for non-PHP files... but you could setup Apache to run any file extension through the PHP parser.

- Shawn

nlopes
Apr 3rd 2004, 10:46 am
Yes, you may configure apache to parse .html files.

compar
Apr 3rd 2004, 11:07 am
I'm not an expert on all of this, but it seems to me that it could be done more simply with ".ssi" files.

Simply write a header.ssi and a footer.ssi file. Then, assuming that the pages have a header and footer now, use some good text editor with a search and replace function and replace each header and footer with the appropriate ".ssi" file?

nlopes
Apr 3rd 2004, 11:09 am
thats an idea, use SSI includes, althought PHP has a better performance and many servers have SSI disabbled.

Pulp
Jun 26th 2004, 6:41 am
You could have your .htaccess file setup like so in the root of the website:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !passthru.php
RewriteRule ^(.*) /passthru.php?file=$1
</IfModule>

Have passthru.php in the root of your web directory as well...

<?php
readfile("header.inc");
readfile($file);
readfile("footer.inc");
?>

header.inc and footer.inc also in the same folder (those of course are your header/footer files).



Hi, when I use this rewrite mod thing, I get only header and footer, no file is displayed. Any suggestions?

digitalpoint
Jun 26th 2004, 7:08 am
Do you have a header and footer file setup, and do you have PHP running?

Pulp
Jun 26th 2004, 7:15 am
Do you have a header and footer file setup, and do you have PHP running?
Yes, I have php and apache with rewrite mod. I did it exactly as you wrote it but everthing I got was header/footer.

digitalpoint
Jun 26th 2004, 7:16 am
No idea to be honest... Someone had the better idea (somewhere in this thread) of doing it at the Apache level, rather than with PHP. Which actually is a better idea..

Ron Williams
Jan 4th 2006, 8:38 pm
I am using digital point's method. It works great, but it applys the header and footer to all file types, including MP3's. Considering I have people hosting there files on my site, this is a problem. How do I restrict it to only specified file types?

n0other
Jan 4th 2006, 9:31 pm
Hello. Take a look at mod_layout. http://tangent.org/index.pl?lastnode_id=478&node_id=362

mod_layout is an Apache module that provides both
a Footer and Header directive to automagically include
output from other URIs at the beginning and ending of a
Web page. It can be used to wrap documents for a
standard look and feel for a site (or to insert banners
on any given document in a site). Currently known to
support mod_perl, PHP and Apache JServ. Should
support just about any type of handler.

Ron Williams
Jan 4th 2006, 10:47 pm
I looked into the module, but I can not use it because I do not have access to httpd.conf. Any other solutions?

FeelLikeANut
Jan 5th 2006, 5:43 pm
thats an idea, use SSI includes, althought PHP has a better performancePHP has better performance only if your pages are already PHP. Otherwise, SSI is just as efficient.

tccoder
Jan 28th 2006, 10:54 pm
Shawn's solution seems like the best way to go, without manually adding to each file.

Also you could use a solution such as index.php?module=pagename

where it would load the pagename.php. Though unless you use a select case that has security risks.

Pm me if u need more help