PDA

View Full Version : How do I include other files in cgi-scripts?



Peo
October 25th, 2002, 20:52
I'm stuck... I can't figure out how to put a javascript code into a cgi-script. I'd also like to know how to include any .php file or html file into the output of a cgi-script (for instance header or footer). Anyone got any ideas on how to do this?

Dusty
October 25th, 2002, 21:01
Just print out JavaScript like any other text:


print '<script language="JavaScript" type="text/javascript"><!--
alert("hi");
//--></script>';

To include a static, local file, open it and print its contents:


open(FILE,'</path/to/file');
while(<FILE>){print $_;}
close(FILE);

To include a file that's either remote or not static, like a PHP file, getprint it using LWP::Simple:


use LWP::Simple;
getprint('http://url/to/file');

I've never tried it, but you might be able to run a local PHP file doing something like:


system('/path/to/php/file');



<edit>
You were talking about Perl, right?
</edit>

YUPAPA
October 25th, 2002, 21:48
#!/usr/bin/perl
use strict;

my $file_to_include = q(/home/path/to/the/file.txt);

print "Content-Type: text/html\n\n";

open(FILE,"<$file_to_include") or die print "Ababa says: I cant open $file_to_include because $!";
print <FILE>;
close(FILE);

__END__