ketaki has asked for the wisdom of the Perl Monks concerning the following question:

i have tried including the contents of a .txt file into a cgi page, and that works. but now i am trying to do sumthing a bit diffrent. i am trying to include the contents of a .shtml into a cgi page. what i get as the output is the contents of the shtml page are outputted but the <!--#include virtual.... doesnt seem to work. i have written the whole path starting from /home/www/.... in the include directive. i have tried replacing include virtual with include file also. but no luck. the output is there but the contents of the file which i have included in the ssi directive are missing on the page when i see it on the browser. i dont want to write the relative path since i dont know from which directory my cgi page will be run by the webserver. thanx...... ketaki update: i am not writting the include command directly in the cgi page (local.cgi). this include command is in the file called forcgi.shtml. And the contents of the file forcgi.shtml are being outputted by the cgi page by file handling. heres my code: heres my code:

#!/usr/bin/perl use lib '/home/www/xxxxxxxxxxxx.org/modules'; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use warnings; use strict; use diagnostics; use declare_dbh; use CGI::SSI; print CGI::header(); my $body; my $dbh=declare_dbh::dbhandle(); my $step =param('step'); my $area =param('area'); if($step != 2) { $body.=<<htmlx; <a target="_blank" href="http://groups.yahoo.com/group/AOL_Delhi/?v=1& +t=search&ch=web&pub=groups&sec=group&slk=1">Sign up to 'xxxxxxxxxxxxx +xxxxxxx' Yahoo Group</a> <br><br><a href="">Upload the latest Info Sheet</a><br><br> <form method="post" action="http://xxxxxxxxxxxxxxxxxx.org/cgi-bin/loca +l.cgi"> Give me details for my Area : <select name="area"> <OPTION VALUE="none" SELECTED>Select an area</OPTION> <option value="505 A B WORKSHOP">505 A B WORKSHOP</option> <option value="A F PALAM">A F PALAM</option> <option value="A F RAJOKARI">A F RAJOKARI</option> <option value="A.G.C.R.">A.G.C.R.</option> <option value="A-3 JANAK PURI">A-3 JANAK PURI</option> <option value="ADRASH NAGAR">ADRASH NAGAR</option> <option value="AIR FORCE STATION TUGALKABAD">AIR FORCE STATION + TUGALKABAD</option> <option value="AJMERI GATE EXTN.">AJMERI GATE EXTN.</option> <option value="ALAKNANDA">ALAKNANDA</option> <option value="ALIGANJ">ALIGANJ</option> <option value="ALIPUR">ALIPUR</option> <option value="AMAR COLONY">AMAR COLONY</option> <option value="AMRIT KAUR MARKET">AMRIT KAUR MARKET</option> <option value="ANAND NIKETAN">ANAND NIKETAN</option> <option value="ANAND PARBAT INDL. AREA">ANAND PARBAT INDL. ARE +A</option> <option value="ANAND PARBAT PO">ANAND PARBAT PO</option> <option value="ANAND VIHAR">ANAND VIHAR</option> <option value="ANANDWAS">ANANDWAS</option> <option value="ANARKALI">ANARKALI</option> <option value="ANDREWSGANJ">ANDREWSGANJ</option> </select> <input type="hidden" name="step" value="2"> <input type="submit" value="Go"> </form> <br><br> In the news : <br><br> <a href="">Upcoming events in Delhi/NCR</a> <br><br>Delhi Central Office:<br><br> htmlx } else { my $sth= $dbh->prepare("select * from chapters where Chapter_name=?"); $sth->execute($area); if ($sth->rows > 0) { $body.= <<htmlx; $area Chapter Information Page:<br><br><br> <br>contact numbers: <br>website : <br>regular followups : <br>sheduled date for next course : <br>recent updates : <br>upcoming events : <br><br><a href="">update chapter info</a> htmlx } else { $body.= "<br><br><br>Sorry, no information for your area is availa +ble yet!" } $body.= <<htmlx; <br><br><a href="http://xxxxxxxxxxxxxx.org/cgi-bin/local.cgi">back to +local information home page</a> htmlx } local $/ = undef; open(file, "/home/www/xxxxxxxxxxxxxxx.org/forcgi.shtml"); my $filedata= <file>; $filedata =~ s/\$body/$body/; close(file); print $filedata;

and below is the code in forcgi.shtml page:

<html> <head> <title></title> <link href="http://xxxxxxxxxxxxxxxxxx.org/styles/style1.css" rel= "sty +lesheet" type="text/css"> </head> <body> <br><br> <!--#include virtual="frame.txt" --> <br><br> <center> <div style="margin:0 auto;width:750px;height:420px;"> $body </div> </center> <!--#include virtual="frame_bottom.txt" --> </body> </html>

and the contents of frame.txt are not being displyed when i view the cgi page online. thanx ketaki

Replies are listed 'Best First'.
Re: including the content of a .shtml file into a cgi page
by moritz (Cardinal) on Jul 15, 2008 at 07:52 UTC
    If you use Apache2, you can tell it to post-process the output from your CGI script with the "includes" filter, which handles SSI:
    AddOutputFilter INCLUDES .cgi

    If you do this, please beware that all of the output is interpreted as a SSI, so you have to take that nobody can enter unescaped data into your script's output. But you should do that anyway in order to prevent cross-site scripting attacks.

Re: including the content of a .shtml file into a cgi page
by pjotrik (Friar) on Jul 15, 2008 at 08:00 UTC

    You'll have to use a module like CGI::SHTML or CGI::SSI to process the shtml file. For static pages, apache takes care of the server side includes; when using CGI, the responsibility is on your handler.

    UPDATE: moritz's advice is probably more elegant, if you're running Apache2

      i dont know if i have apache2. dont know hw to find dat out. i added the use cgi::ssi statement. but it still doesnt seem to work. the cgi file name is local.cgi. the file which i am reading within the local.cgi page is called forcgi.shtml. and the contents of frame.txt are being included in the directive include command written on the forcgi.shtml page. the rest of the contents of the file forcgi.shtml are being displayed, but the include command written on the forcgi.shtml page which includes the contents of frame.txt , doesnt seem to work. so i cant see the contents of frame.txt, on the local.cgi page. the problem could even be the virtual or file tag, in the include command on the forcgi.shtml page. forcgi.shtml and frame.txt are both in the same directory, while the local.cgi page is inside the cgi-bin directory. i dont even know from which directory my webserver runs my cgi pages. thanx... ketaki

        If you don't know what webserver you're running, how are we supposed to know?

        Also, "doesn't seem to work" is not a very specific error description.

        See How (Not) To Ask A Question