Forum Moderators: coopster & phranque

Message Too Old, No Replies

having include problems

Including html file in cgi script

         

Receptional Andy

1:39 pm on Mar 14, 2003 (gmt 0)



I'm trying to include header and footer html files in a cgi script, but nothing I try seems to do the trick.

Last thing I used in the cgi was:

$ifile ="/path/top.html";

open FILE, $ifile;
@o=<FILE>;
close FILE;
foreach (@o) {
print $_;
}

I'm sure this question has been asked before, but I can't find anything suitable on the site.

If someone could point me in the right direction it would be much appreciated.

Is there not a simple include command I can use like in PHP?

dive into perl

4:40 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



Hi Andy,

#!/usr/bin/perl -w
use strict;

$ifile = "/path/top.html";

open (TEMP, $ifile) or die("Can't open file $ifile");
$contents = join ('', <TEMP>);
close (TEMP);

print $contents;

Receptional Andy

4:51 pm on Mar 14, 2003 (gmt 0)



Thanks for that. Any idea why I get an internal server error with the above code? I don't have access to error logs to find the reason.

Receptional Andy

4:58 pm on Mar 14, 2003 (gmt 0)



OK, got to the error log and I can see this:

Bad header=Global symbol "$ifile" require: /usr/cgiwrap/cgiwrap

dive into perl

5:07 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



My mistake,

As the code uses strict, then the following will work.

#!/usr/bin/perl -w
use strict;

my $ifile = "/path/top.html";

open (TEMP, $ifile) or die("Can't open file $ifile");
my $contents = join ('', <TEMP>);
close (TEMP);

print $contents;

Receptional Andy

5:14 pm on Mar 14, 2003 (gmt 0)



Thanks again for the help.
Sorry to keep asking, but i'm not too great with perl.

I now get this error:

Bad header=Bareword found where operator : /usr/cgiwrap/cgiwrap

andreasfriedrich

5:41 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to enclose the bareword TEMP in quotes.


#!/usr/bin/Perl [perl.com] -w
use strict;
#
my $ifile = "/path/top.html";
#
open ('TEMP', $ifile) or die("Can't open file $ifile: $!\n");
my @contents = <TEMP>;
close ('TEMP');
#
print @contents;

Andreas

Receptional Andy

5:55 pm on Mar 14, 2003 (gmt 0)



Thanks Andreas - progress. I don't get the internal server error any more, however, now I get 'no such file or directory'

How should I write the path to the include file?

The cgi script is in domain.com/cgi-bin and my header file is in domain.com/inc/top.html

No path I try works. I've tried ../inc/top.html /inc/top.html and also the full unix path /home/myfolders/etc/web/inc/ and even [domain.com...]
All of these generate the file not found error. Help! :(

andreasfriedrich

6:02 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try

my $ifile = "$ENV{DOCUMENT_ROOT}/inc/top.html";

Andreas

Receptional Andy

6:04 pm on Mar 14, 2003 (gmt 0)



hmm, still get the error. So it must be a different file that isn't found - this bit perhaps?

open ('TEMP'

Why would this not work? Could it be a server config problem?

Receptional Andy

6:15 pm on Mar 14, 2003 (gmt 0)



OK, I'm being a dumbass. File not found was because of the capital 'P' in perl above ;)

Still not working though. I just realised that I have some PHP in the inc files, so I guess this whole idea is doomed to failure. Unless I can include a PHP file in a CGI script?

Receptional Andy

6:21 pm on Mar 14, 2003 (gmt 0)



I made a test include file:

<html>
<head>
<title>test</title>
</head>
<body>
woohoo!
</body>
</html>

I get the error:

malformed header from script. Bad header=<html>

This is really starting to get to me ;)

Key_Master

6:25 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




print "Content-type: text/html\n\n";
print $contents;

dive into perl

6:26 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



I had assumed this was a code snippet from a larger script, if you have copied andreasfriedrich example then you will need

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

after the use strict line, so that it sends the correct header.

[edit]Typed too slow, key master beat me to it :-)[/edit]

andreasfriedrich

6:31 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>capital 'P' in Perl [perl.com]

Yeah, thatīs my auto linking tool again. It turns p-h-p to PHP [php.net] and p-e-r-l to Perl [perl.com].

>>Unless I can include a PHP [php.net] file in a CGI script?

You could run the include file through the PHP [php.net] binary. Just change the line defining the filename to read like this:

my $ifile = "php4 -q /path/top.html¦";

Notice the pipe symbol after the filename. You need to adjust the name of the PHP [php.net] binary and might want to use an absolute path to it. The -q keeps PHP [php.net] from writing any headers.

Andreas


Note: Make sure to replace "¦" with a solid vertical pipe.

Receptional Andy

6:53 pm on Mar 14, 2003 (gmt 0)



Thanks for your help everybody. You'll have to forgive me if I keep making basic mistakes.

Got includes working with flat html files (woohoo!)
now i'm trying to pass my html include file (with php scripts) through the PHP binary. The output i'm getting is:

X-Powered-By: PHP/4.0.6 Content-type: text/html use strict; # my $ifile.....

Seems to be just printing out the perl script. Any ideas?

This is what I have:

#!/usr/bin/perl -w
use strict;
#
my $ifile = "/usr/bin/php -q ../inc/test.html¦";
#
open ('TEMP', $ifile) or die("Can't open file $ifile: $!\n");
my @contents = <TEMP>;
close ('TEMP');
#
print "Content-type: text/html\n\n";
print @contents;

andreasfriedrich

7:33 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Test running the PHP [php.net] script separately. It seems your PHP [php.net] binary does not understand the -q option. Try PHP [php.net] -h for information on how to call it. If that works, try it from within the Perl [perl.com] script again. Try using absolute paths.

The example I posted works for me using PHP [php.net] 4.1.2.

Andreas

Receptional Andy

8:40 pm on Mar 14, 2003 (gmt 0)



Not having much luck here. Maybe there's a simpler way to do what I need. It's a PHP site using includes of header and footer files (that also contain inline php scripts). However, my problem is that I also use cgi scripts occasionally. I can include the cgi script in a php file really esily, but if I click on any links generated by the cgi script, I lose all the formatting.

How do I get my cgi scripts to be displayed with my site header and footer files. Is my best bet to stick at the include method above, or is there another way?