PDA

View Full Version : PHP inoperable from within CGI folder?


Ammoratti
07-26-2006, 12:10 AM
My eager thanks to mlseim for helping me out earlier!
I'm finding I really enjoy learning these techniques in web authoring, though frankly, I'm still trying to get the grasp of some of the basics. =]

I've encountered another obstacle. Virtually my entire site consists of .php files so that I could make heavy use of the INCLUDE feature. This has saved me countless hours in updating navigational links and other repetitive coding routines.

However, I wanted to make use of a CGI FAQ script which calls both a "header" and "footer" .html file and appends them above and below CGI output respectively. My site currently resides on a UNIX server; all Perl scripts of course are to be installed within the cgi-bin folder.

The problem though is my PHP includes (inserted in the "header" and "footer" html files) quit working. Unless the "header" and "footer" files are sitting outside the CGI-BIN folder, PHP includes just will not operate. And unless these two files are sitting in the same directory as the CGI script, they will not append to the CGI output.

So am I stuck between a rock and a hard place? I'm wondering if it's a permissions issue. Or perhaps there's CGI code that can replace the PHP INCLUDE command that can work from within the CGI-BIN folder?

Thanks for all your help!!!

KevinADC
07-26-2006, 05:28 AM
Put the header/footer files in a folder outside the cgi-bin. Then use that for your php files and use the path to the files in your CGI scripts. The CGI scripts can read files in any folder, you just need to use the correct path.

Ammoratti
07-26-2006, 11:49 AM
Put the header/footer files in a folder outside the cgi-bin. Then use that for your php files and use the path to the files in your CGI scripts. The CGI scripts can read files in any folder, you just need to use the correct path.


Thanks, Kevin.
Your assurance here prodded me to try something more, which did work.
Here's a sampling of what was attempted before....


# Path and file name of header:
$header_file = 'http://www.mysite.com/faq/header.php';
# Path and file name of footer:
$footer_file = 'http://www.mysite.com/faq/footer.php';




# Path and file name of header:
$header_file = '/faq/header.php';
# Path and file name of footer:
$footer_file = '/faq/footer.php';



Both attempts failed. It wasn't until I tried the following that the script succeeded:


# Path and file name of header:
$header_file = '../../faq/header.php';
# Path and file name of footer:
$footer_file = '../../faq/footer.php';



Very strange. Picky language that Perl is. Thanks again!

Ammoratti
07-26-2006, 12:02 PM
Ah, spoke too soon.

Here's the INCLUDE code I'm using in the header.php file located in the "/faq" directory:


<?php include("http://www.mysite.com/leftnav.inc"); ?>


Given your advice, the CGI script now successfully finds and processes all the code above this line, including applying the default external .css stylesheet, but still fails to include the code contained within the "leftnav.inc" file.

I may try using a SSI and changing the file to .shtml instead in the morning. Meanwhile, got any ideas?

mlseim
07-26-2006, 02:52 PM
Ammoratti ...

some possibilities:

1)
=============================================
You could look at something like this Perl Module:

http://sam.tregar.com/html_template.html

It's sort of like the PHP include, but it reads in the HTML within
your Perl script instead. Requires header footer modifications.
=============================================

2) using SSI
=============================================
You'll discover the same thing as PHP. You can't put the
SSI includes in your Perl script.
=============================================

3)
=============================================
This is the method I would try first. Not quite as elegant as
a module, but you can use your existing HTML files without
any modifications.

print "Content type=text/html\n\n";
#
$filename = "/path/to/header.htm";
#
open (HTM,"$filename");
@lines = <HTM>;
close (HTM);
foreach $line (@lines) { print "$line"; }
#
#
# do whatever comes between header and footer
#
#
$filename= "/path/to/footer.htm";
#
open (HTM,"$filename");
@lines = <HTM>;
close (HTM);
foreach $line (@lines) { print "$line"; }

FishMonger
07-26-2006, 06:32 PM
Here's a little more effecient method of mlseim's 3rd option.

open (HTM,"$filename") || die $!;
print <HTM>;
close HTM;

You can also look into using one or both of these modules.
http://search.cpan.org/~karasik/PHP-0.09/PHP.pm
http://search.cpan.org/~esummers/PHP-Include-0.2/lib/PHP/Include.pm

But mlseim's 1st option is a better choice.

If you want an even more powerful and flexible approach, you can use the Template Toolkit or Mason

http://search.cpan.org/~abw/Template-Toolkit-2.15/lib/Template/Toolkit.pod
http://www.template-toolkit.org/
http://www.masonhq.com/

KevinADC
07-26-2006, 08:01 PM
this:

<?php include("http://www.mysite.com/leftnav.inc"); ?>

reads the file from the URL, this is something perl does not do as easily as PHP does. But you can use the machine path to those files in your perl scripts instead of the URL, might look like:

my $header = '/home/sitename/header.txt';
my $footer = '/home/sitename/footer.txt';

open (HEADER, ">$header") or die "Can't open $header: $!";
print (HEADER);
close (HEADER);

here the rest of the page is printed

open (FOOTER, ">$footer") or die "Can't open $footer: $!";
print (FOOTER);
close (FOOTER);


there is nothing special about HEADER and FOOTER they are just file handles, you could use any name for the filehandles.

FishMonger
07-26-2006, 08:15 PM
Kevin, I think you meant this:

open (HEADER, "<$header") or die "Can't open $header: $!";
print <HEADER>;
close (HEADER);

here the rest of the page is printed

open (FOOTER, "<$footer") or die "Can't open $footer: $!";
print <FOOTER>;
close (FOOTER);

KevinADC
07-26-2006, 09:12 PM
Kevin, I think you meant this:
.....

Yes I did, thank you Fish! :o :thumbsup: