PDA

View Full Version : Adding footer through .htaccess


Due
10-26-2001, 05:38 PM
Is it possible to get ALL html pages to include a footer through .htaccess ?
And how would the code look like?

Jer
10-26-2001, 07:42 PM
I'd like to know too.

ComaToast
10-26-2001, 09:02 PM
You can do it with SSI.
Most ppl use "AddType" and "AddHandler". If you parse all files
for SSI directives(and some pages do not have SSI directives)
it slows your site. Plus you have to rename extensions to .shtml.
Use the XBitHack directive instead.


httpd.conf
Options +Includes
XBitHack on

.htaccess
AllowOverride Options
Options +Includes
XBitHack on

Where ever you want the footer/header
<!--#include file="header.html" -->

Read up on "IncludesNOExec" if your the paranoid type.

Due
10-26-2001, 09:52 PM
Problem is I wanna do it without adding <!--#include file="header.html" --> to all the html pages.

skymouse1
10-26-2001, 10:06 PM
Here's a way to do it. I daresay there are more effecient ways, but this works.

In .htaccess:


AddHandler spam-html .html
AddHandler spam-html .htm
Action spam-html /spam.cgi



spam.cgi:

#!/usr/bin/perl


$spam=<<END;

<!--------------- start of footer --------------->

<p>Hi! I'm a piece of footer html that willl appear on every page!!!</p>

<!--------------- end of footer --------------->
END

$pg=$ENV{PATH_TRANSLATED};

$hdr="content-type: text/html\n\n";

print $hdr;
open (T,"<$pg");
while (<T> ) {
s/\<\/BODY\>/$spam\n\<\/BODY\>/i;
print;
}
close T;
exit;


:cool:

theo
10-26-2001, 10:27 PM
skymouse1 you just got 5 stars :)

what will be the code for header or both header and footer?

Due
10-27-2001, 06:13 AM
5 star vote from me also :)
Thanks man. Do you know how much CPU it will eat up?

run
10-27-2001, 08:02 AM
Perl may eat cpu. Use C. ICQ me if you need help.

Zyber [SharkSkills.com]
10-27-2001, 10:08 AM
Due, Perl eats your CPU for breakfast, hehe
Use PHP instead for the script above..

Much0S
10-27-2001, 12:05 PM
Originally posted by Zyber [SharkSkills.com]:
<STRONG>Due, Perl eats your CPU for breakfast, hehe
Use PHP instead for the script above..</STRONG>

true but PHP eats your Memory for breakfast :D - Use C !!! Its best, and most programmers( even mediocre ones ) should be able to get this right :) It's only 10 lines ;)

something like (haven't done it for a long time so bear with me)

this code should be fastest possible (however not very flexible):

#include "stdio.h"

#define FOOTER "[h1][A HREF=http://www.spam.com/ TARGET=_top&gt;SPAM IT IS!&lt;/A&gt;&lt;/H1&gt;\n"

int main( int argc, char **argv ) {
printf(FOOTER);

return 0;
}

XP
10-27-2001, 12:27 PM
First make your Header & Footer in one line with notepad (del, end, del, end buttons ..)
also this code should be better MuchOs :)

#include "stdio.h"
#define FOOTER "[h1][A HREF=http://www.spam.com/ TARGET=_top&gt;SPAM IT IS!&lt;/A&gt;&lt;/H1&gt;\n"
void main() {
printf(FOOTER);
return 0;
}


also, you can make all htm files as SSI and just print &lt;!-- include ..... code with this small C program in header & footer and not mess with C language (for printing header & footer)

like this

#header.c
#include "stdio.h"
#define HEADER "&lt;!--#include file="/home/www/header.html" --&gt;"
void main() {
printf(HEADER);
return 0;
}

and for Footer

#footer.c
#include "stdio.h"
#define FOOTER "&lt;!--#include file="/home/www/footer.html" --&gt;"
void main() {
printf(FOOTER);
return 0;
}


you can compile that shit with
gcc -o header.cgi header.c
gcc -o footer.cgi footer.c

simple eh?

Groovy
10-27-2001, 12:45 PM
easy job...
:D

Due
10-27-2001, 01:04 PM
Run helped me out by writing a code that looks good :) will let you guys know if it works once I got it tested :)

Much0S
10-27-2001, 02:00 PM
[QUOTE]Originally posted by XP:
[QB]First make your Header & Footer in one line with notepad (del, end, del, end buttons ..)
also this code should be better MuchOs :)

Actually XP the way you have it is REALLY SLOW. Because Apache needs to scan the HTML files for SSI's. As I said, it's not flexible, but it's fastest possible. Don't ever doubt my skills :rolleyes: :p

skymouse1
10-27-2001, 09:26 PM
Originally posted by theo:
<STRONG>skymouse1 you just got 5 stars :)

what will be the code for header or both header and footer?</STRONG>

Cheers for the stars!

Code for header and footer follows, although it is very quick and dirty and could be made more efficient:


#!/usr/bin/perl

$header=&lt;&lt;END;
&lt;!---- start of header ---------------&gt;
&lt;p&gt;Hi! I'm a piece of header html that willl appear on every page!!!&lt;/p&gt;
&lt;!---- end of header ---------------&gt;
END

$footer=&lt;&lt;END;
&lt;!---- start of footer ---------------&gt;
&lt;p&gt;Hi! I'm a piece of footer html that willl appear on every page!!!&lt;/p&gt;
&lt;!---- end of footer ---------------&gt;
END
$pg=$ENV{PATH_TRANSLATED};
$hdr="content-type: text/html\n\n";
print $hdr;
open (T,"&lt;$pg");
while (&lt;T&gt; ) {
s/\&lt;BODY\&gt;/\&lt;BODY\&gt;\n$header/i;
s/\&lt;\/BODY\&gt;/$footer\n\&lt;\/BODY\&gt;/i;
print;
}
close T;
exit;


If the pages are long, it would be advisbale to split the while-loop into seperate loops for the header and the footer. But you get the idea. :)

skymouse1
10-27-2001, 09:30 PM
Originally posted by run:
<STRONG>Perl may eat cpu. Use C. </STRONG>

I agree C is nice and fast. But with mod_perl compiled into Apache nowadays, I wonder how much the real difference in efficiency is?

SM