Running PHP scripts from Perl

P

P E Schoen

I was able to run a single PHP script from Perl by using
print '<html><body><iframe src="HTMLfilter.php"></iframe></body></html>' .
"\n";

But when I set up a loop to apply it to all the CGI Input variables, it
seemed that the PHP script ran after the Perl script had finished, and it
only acted on the last environment variable. I also tried using <script
language="text/php" src="HTMLfilter.php"> but no joy.

The loop is as follows:

my %in = Vars(); # Get the CGI input variables
while ( (my $key, my $value) = each (%in) ) {
# Write to Raw.htm
print fLog "Raw: $key -> $in{$key}\n";
open (fHTMLraw, '>', $HTMLrawfile)
or HTMLdie ("File error: $!");
print fHTMLraw $value;
close fHTMLraw;
print '<html><body><iframe src="HTMLfilter.php"></iframe></body></html>'
.. "\n";
# sleep(1);
if (-e $HTMLpurefile){
open (fHTMLpure, '<', $HTMLpurefile);
$in{$key} = <fHTMLpure>;
close fHTMLpure; }
else {
$in{$key} = 'Purifier error';
print fLog "Purifier error"; }
print fLog "Pure: $key -> $in{$key}\n";
# sleep(1);
}

I had used the sleep() but it just delayed the script and the iframes
appeared after the script had finished.

I found this article about using the PHP::Interpreter module:
http://www.linuxjournal.com/article/9282

I was able to download and unzip the package but I was unable to install it
using ppm install PHP::Interpreter. The article was aimed at Linux and I'm
not sure if it applies to Win7. I would like to be able to use the PHP
HTMLpurifier rather than the Perl equivalent if possible.

Another option may be to assemble the various inputs into a single HTML
document and then run it through the purifier. The problem is that I am
storing the input variables in a database, although I could just use an
autoincrement ID and a DateTime for indexing, and combine the other
variables into a single field with the complete HTML. But that would not
lend itself to changing the appearance of the HTML page for display of all
the data.

It would probably be OK to use the "Declaw" and "Defang" utilities to clean
up the raw entries, and then run the final rendered HTML through the PHP
purifier to catch anything missed.

Thanks,

Paul
 
L

Luuk

I was able to run a single PHP script from Perl by using .....


print '<html><body><iframe
src="HTMLfilter.php"></iframe></body></html>' . "\n";
# sleep(1);

This will NOT run your php script. it will just produce some output to
your perl script

$result = `php HTMLfilter.php`;
print '<html><body><iframes>$result</iframe></body></html>' . "\n";

Might do what you want (i'm not a Perl expert... ;)
 
J

Jürgen Exner

P E Schoen said:
I was able to run a single PHP script from Perl by using
print '<html><body><iframe src="HTMLfilter.php"></iframe></body></html>' .
"\n";

No you weren't. That is a simple print() statement and it doesn't run
anything whatsoever. Maybe somewhere down the line some web server
decides to interpret that output as a command and run something, but
that is a whole different story and has nothing to do with Perl.

If you want to run an external command from Perl you have to use e.g.
system() or backticks or something similar.

jue
 
P

P E Schoen

"Jürgen Exner" wrote in message
No you weren't. That is a simple print() statement and it doesn't run
anything whatsoever. Maybe somewhere down the line some web
server decides to interpret that output as a command and run
something, but that is a whole different story and has nothing to do
with Perl.

Yes, that was definitely the wrong approach. I found that somewhere as I was
searching:
http://www.webmasterworld.com/forum13/3512.htm
http://www.theukwebdesigncompany.com/articles/article.php?article=159
http://stackoverflow.com/questions/104516/calling-php-functions-within-heredoc-strings
If you want to run an external command from Perl you have to use
e.g. system() or backticks or something similar.

OK, I tried some of the other suggestions and I also found information about
running PHP from Perl as well as running Perl from PHP. But they required
additional modules or extensions to be downloaded and installed and it
seemed very complicated and I was not able to get that to work:
http://devzone.zend.com/article/1712
http://www.cpan.org/modules/by-module/PHP/
http://pecl.php.net/package/perl

But, I also tried your suggestion of system(), and it works fine, except
that a command window flashes for every invocation.
http://perl.about.com/od/programmingperl/qt/perlexecsystem.htm

I removed the echo from the PHP script because it also echoed the password.
I really did not need the results echoed because the script simply reads a
"Raw.htm" file, processes its contents through the HTMLpurifier, and then
writes the results to "Pure.htm". It also writes to a "PHPlog.txt" file for
debugging purposes. The Perl script takes it from there. So what I have is:

while ( (my $key, my $value) = each (%in) ) {
# Write to Raw.htm
print fLog "Raw: $key -> $in{$key}\n";
open (fHTMLraw, '>', $HTMLrawfile)
or HTMLdie ("File error: $!");
print fHTMLraw $value;
close fHTMLraw;
system( '..\php\php HTMLfilter.php' );
if (-e $HTMLpurefile){
open (fHTMLpure, '<', $HTMLpurefile);
$in{$key} = <fHTMLpure>;
close fHTMLpure; }
else {
$in{$key} = 'Purifier error';
print fLog "Purifier error"; }
print fLog "Pure: $key -> $in{$key}\n";
}

I also looked into backtick and exec() but they did not seem to be what I
needed:
http://www.troubleshooters.com/codecorn/littperl/perlspaw.htm
http://www.wellho.net/mouth/324_The-backtick-operator-in-Python-and-Perl.html
http://perldoc.perl.org/perlfaq8.html#What's-wrong-with-using-backticks-in-a-void-context?

And a more detailed discussion of all methods (but in unix):
http://www.techrepublic.com/article...aches-let-you-grab-data-for-unix-apps/1050920

Thanks, everyone. Sorry to ask such stupid questions, but the answer proved
to be very simple.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,736
Members
45,708
Latest member
RenaldoFor

Latest Threads

Top