Forum Moderators: coopster & phranque

Message Too Old, No Replies

PHP includes in a Perl generated HTML file. Can it be done?

Is it possible to include PHP includes in an HTML file generated by Perl?

         

douglas in philly

2:03 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



Hello,

I work for a small college, and often write Perl scripts that create web pages and interface with MySQL databases.

We recently redid our main college website, and there is a lot of PHP in the new site design. I want to include the PHP includes in my Perl documents, but I haven't been able to get it to work. The reason for this, I believe, is because the files I create have an extension of .pl or .cgi. If I create an HTML file with PHP embedded in it, and give the file the extension .php, it works just fine. But the way I do it, my files look something like this:

*************Code Follows***************

#!/usr/local/bin/perl

$¦ = 1;

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

print <<end_of_html_form;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My College: Contacts</title>
</head>
<body>

<?php include ('/webdata/docroot/includes/template/accessibility.php');?>

</body>
</html>

end_of_html_form

*************End of Code***************

Let's say that code is in a file called, "hello.pl". When I visit [mycollege.edu...] the php includes don't work.

If I created the same HTML file, but instead of making it a Perl file that generates the HTML, just made it straight HTML, then the PHP includes would work.

Any ideas of what I can do to make it work?

Thanks.........Doug

rocknbil

5:26 pm on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Doug, one solution is to do as you said - write the output to a file. This is because when the server gets a request for a file with a .php extension, it gets parsed by the php parser - that is, as it reads in the file, it looks for the php commands to execute. Since your original is perl it can't execute PHP inline (that I know of.)

Normally I would say you can do an include without using PHP, using perl, or more appropriately Server Side Includes, but since your included file appears to be a php script you will want it to be in a PHP file so it executes as it's being parsed. There is a possibility you can use the perl exec command on the include, but I don't know.

perl_diver

11:38 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



I have never used it:

[search.cpan.org...]

Dabrowski

2:58 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had the same problem recently, but with an SHTML include, that's the <!-- #include ... --> thing, but is also server based. When the page is served the server substitutes those lines for the included files.

The scenario I had was for an enquiry form, the Perl script opened a SHTML file and outputted it as a success page for the user. The page included all the site standard menus and things, and I didn't want to lose this functionality.

I wrote this simple function which you could very easily adapt, it searches a file, in this case SHTML, copies it, but if it comes upon a #include line it splices that code in instead (by calling itself). Also handles nested includes.

sub SHTMLtoHTML {
my $file = shift;
$file =~ s/^\///;

open FILE, $file;
my @inhtml = <FILE>;
my @outhtml;
close FILE;

foreach( @inhtml) {
my $includefile = $1 if( m/<!-- #include virtual="(.*?)" -->/);

if( $includefile) {
my @includehtml = SHTMLtoHTML( $includefile);
push( @outhtml, $_) foreach( @includehtml);

} else {
push( @outhtml, $_);
}
}

return @outhtml;
}

phranque

3:45 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I had the same problem recently, but with an SHTML include...

you can use CGI::SSI for this, which also presumably supports the other SSI directives such as conditionals and variable assignment.

Dabrowski

4:26 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This was just a quick fix, took me all of 10 mins.

What does CGI::SSI do? Just that? Make substitutes for SSI codes? Ideally there should be a way to tell the server to process this PL file as an SHTML file.

phranque

10:11 am on Oct 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



What does CGI::SSI do?

CGI::SSI [search.cpan.org] imitates Apache's mod_include [httpd.apache.org].

Ideally there should be a way to tell the server to process this PL file as an SHTML file.

you could try serving the document with mime type text/x-server-parsed-html instead of text/html.
you might have to enable includes [httpd.apache.org] for this to work.