Running PHP scripts from Perl

Discussion in 'Perl Misc' started by P E Schoen, Jan 26, 2011.

  1. P E Schoen

    P E Schoen Guest

    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
    P E Schoen, Jan 26, 2011
    #1
    1. Advertising

  2. P E Schoen

    Luuk Guest

    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
    Luuk, Jan 26, 2011
    #2
    1. Advertising

  3. "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";


    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
    Jürgen Exner, Jan 26, 2011
    #3
  4. P E Schoen

    P E Schoen Guest

    "Jürgen Exner" wrote in message
    news:...

    > "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";


    > 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
    P E Schoen, Jan 26, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mpc
    Replies:
    1
    Views:
    2,451
    Michael Palmer
    Sep 16, 2008
  2. Rajive Narain
    Replies:
    0
    Views:
    1,558
    Rajive Narain
    Sep 18, 2009
  3. Ignoramus6539
    Replies:
    2
    Views:
    193
    Colin McKinnon
    Aug 30, 2006
  4. Replies:
    13
    Views:
    513
    Anno Siegel
    Sep 10, 2007
  5. M. Strobel
    Replies:
    1
    Views:
    201
    The Natural Philosopher
    Mar 5, 2013
Loading...

Share This Page