Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Running PHP scripts from Perl

Reply
Thread Tools

Running PHP scripts from Perl

 
 
P E Schoen
Guest
Posts: n/a
 
      01-26-2011
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

 
Reply With Quote
 
 
 
 
Luuk
Guest
Posts: n/a
 
      01-26-2011
On 26-01-11 10:46, P E Schoen wrote:
> 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...

> Thanks,
>
> Paul



--
Luuk
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-26-2011
"P E Schoen" <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
P E Schoen
Guest
Posts: n/a
 
      01-26-2011
"Jürgen Exner" wrote in message
news:(E-Mail Removed)...

> "P E Schoen" <(E-Mail Removed)> wrote:
>> 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.


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...hp?article=159
http://stackoverflow.com/questions/1...eredoc-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/programming...execsystem.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/codec...l/perlspaw.htm
http://www.wellho.net/mouth/324_The-...-and-Perl.html
http://perldoc.perl.org/perlfaq8.htm...oid-context%3f

And a more detailed discussion of all methods (but in unix):
http://www.techrepublic.com/article/...x-apps/1050920

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

Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: getting perl and php to talk to each other, was Re: randomlysorting files in php M. Strobel Perl Misc 1 03-05-2013 07:20 PM
PHP Training Institute In Delhi, Live Projects on PHP. Short TermPHP Courses, PHP Scripts, PHP Training with Live Projects. Rajive Narain Java 0 09-18-2009 10:47 AM
CGIHTTPServer webserver running php scripts mpc Python 1 09-15-2008 11:28 PM
What is required for perl scripts to run correct when launched from rc scripts on HPUX 11? deanjones7@gmail.com Perl Misc 13 09-10-2007 11:58 AM
PHP/Perl/Unix Virus: delete config.php files asap Ignoramus6539 Perl Misc 2 08-30-2006 08:22 PM



Advertisments