Forum Moderators: coopster & phranque

Message Too Old, No Replies

Including A CGI Script Via .HTACCESS

Including A CGI Script Via .HTACCESS

         

Hard_Drive

7:05 am on Jan 3, 2003 (gmt 0)

10+ Year Member



OK Here goes...

I used to have a small cgi script that rested in my cgi-bin
it would include a header and a footer (specified in the cgi-script) On every page inside a directory.
You only had to put a line of code in your .htaccess in that directory to call the cgi script from inside the cgi-bin. The cgi script would replace the <body> tag or the </body> tag with the header or footer pages. Almost like SSI but without having to edit every page inside a directory. I hope this makes since..

Does anyone know of such an animal?
Does anyone happen to have this script in their bag "o" tricks?

Thank You...
There is a lot of great reading in here!

[edited by: Woz at 11:50 pm (utc) on Jan. 5, 2003]

andreasfriedrich

1:42 am on Jan 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Hard_Drive,

I hope you havenīt given up waiting. ;)

The best way would be to write a handler using mod_perl to add header and footer.

If you do not have mod_perl then you could do any of the following:

If you are using PHP use the auto_append_file and auto_prepend_file directives [php.net] to automatically append and prepend a file. You can change those directives from within a .htaccess file [php.net] using Apacheīs php_value directive.

Or you could use this CGI approach:

Options +ExecCGI 
AddHandler cgi-script .pl
Action add-fh /aaron-carter/fh.pl
AddHandler add-fh .html

#!/usr/bin/perl 
#
use strict;
#
print <<END;
Content-type: text/html\r\n\r\n
header
END
#
open 'IN', "<$ENV{PATH_TRANSLATED}";
print <IN>;
#while (<IN>) {
#s/<body>/header/;
#s!</body>!footer!;
#print;
#}
#
print <<END2;
footer
END2

Andreas

Hard_Drive

5:00 am on Jan 6, 2003 (gmt 0)

10+ Year Member



Thanks! andreasfriedrich

I take it that this little snippet goes into my .htaccess?

Options +ExecCGI
AddHandler cgi-script .pl
Action add-fh /aaron-carter/fh.pl
AddHandler add-fh .html

With this changed to my path to the .cgi
Action add-fh /aaron-carter/fh.pl?

And then this is the cgi portion that goes into my cgi-bin?
with header and footer replaced with my HTML Code...?

#!/usr/bin/perl
# use strict;
# print <<END; Content-type: text/html\r\n\r\n header END
# open 'IN', "<$ENV{PATH_TRANSLATED}"; print <IN>;
#while (<IN>) {
#s/<body>/header/;
#s!</body>!footer!; #print; #}
# print
<<END2;
footer
END2

Right? I hope so!
Thanks for replying!

andreasfriedrich

5:22 am on Jan 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The script code contains two versions of this little script.

The first version reads the whole file that was originally requested and simply sends it to the user.

#!/usr/bin/perl  
#
use strict;
#
print <<END;
Content-type: text/html\r\n\r\n
! hard coded header within script!
END
#
open 'IN', "<$ENV{PATH_TRANSLATED}";
print <IN>;
#
print <<END2;
! hard coded footer within script!
END2

The second version replaces <body> and </body> with header and footer.

#!/usr/bin/perl  
#
use strict;
#
print <<END;
Content-type: text/html
# additional header fields go here
\r\n\r\n
END
#
open 'IN', "<$ENV{PATH_TRANSLATED}";
while (<IN>) {
s/<body>/! hard coded header!/;
s!</body>!? hard coded footer?!;
print;
}

With this changed to my path to the .cgi
Action add-fh /aaron-carter/fh.pl?

Yes. Yes, the first four rows belong into your .htaccess file.

I should have been more concise but you were ok in figuring out everything ;)

Andreas