Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Make PHP Execute in CGI output

by hakkr (Chaplain)
on Dec 01, 2001 at 18:39 UTC ( #128865=perlquestion: print w/replies, xml ) Need Help??

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

Our web design team has an affinity for PHP. This means I am often provided with HTML pages containing PHP. I am then tasked with using these pages in my CGI scripts. The below code is a vain and doomed attempt to do so.

print "Content-type: text/html\n\n"; print <<HTML; <?php include('header.php'); ?> <body> <!--rest of doc--> </body> </html> HTML

But when I print PHP code it does not get executed and gets printed literaly. This is because the CGI is bypassing the Apache PHP processor when printing to STDOUT.

So I currently have to recode all the PHP into Perl, This is a great hassle when they start getting more fancy than a file include

How can I get the PHP executed before printing it to STDOUT?

Replies are listed 'Best First'.
Re: Make PHP Execute in CGI output
by miyagawa (Chaplain) on Dec 01, 2001 at 18:49 UTC
    Some of that I can imagine:
    • write mod_perl handler (maybe PerlTransHandler) to output your PHP code into temporary directory, translate rerquest's filepath to the one in tmp, and set main content handler to php.
    • use command line PHP to process code.
    • use php-perl by dougm, the author of mod_perl.

    Tatsuhiko Miyagawa

      • We are not yet running under mod_perl. Converting all our scripts to be able to do so is way up there on my todo list
      • All our PHP installations are of the Apache Server module variety so we have no command line PHP available.
      • Is there any documentation for php-perl?
Re: Make PHP Execute in CGI output
by belg4mit (Prior) on Dec 01, 2001 at 20:05 UTC
    Apache 2.0 will make this much easier. Until then something else to perhaps try is placing mod_cgi before mod_php in your module_list (before means @ the bottom), to give it a higher precedence. Usually mod_php gets executed long before mod_cgi.

    perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Make PHP Execute in CGI output
by DaWolf (Curate) on Dec 01, 2001 at 23:27 UTC
    Ok, maybe it's not the easier way, but I think it will work:

    1) First create a file and put the code into it.
    2) Make the browser load the file.

    For an example (using your code):
    open (THP, ">foo.php"); # THP means TemporaryPHP hehe print THP>>The_End; <!-- Initial HTML code--> <?php include('header.php'); ?> <body> <!--rest of doc--> </body> </html> The_End close (THP); print "Location: http://URL_to_the_file/foo.php\n\n";
    Best regards,

    Er Galv„o Abbott
    a.k.a. Lobo, DaWolf
Re: Make PHP Execute in CGI output
by giulienk (Curate) on Dec 02, 2001 at 01:08 UTC
    Maybe you should check for Apache::SSI or, as you don't use mod_perl, CGI::SSI. I didn't get as far to look at CGI::SSI code but i suppose it gets the file you want to include via LWP and then include it in your page. I think that's not really optimized but will do the work.


Re: Make PHP Execute in CGI output
by perrin (Chancellor) on Dec 02, 2001 at 00:32 UTC
    You probably could solve this problem using one of the ways suggested already, but if you care about performance of your app, don't do it. It's always slow to do this kind of interaction between multiple interpreters. You'd be better off teaching them some SSI syntax (there are modules for simulating SSI on CGI output) or in-line perl.

    If you switch to mod_perl, you might be able to rig something up that would be no worse than your current CGI in terms of performance, but it's still probably not worth the trouble ultimately.

Re: Make PHP Execute in CGI output
by Anonymous Monk on Dec 02, 2001 at 02:49 UTC
    If you are not getting any returned values, like a counter or something, you could do any of the following:
    print "<script src=\"counter.php\"></script>"; #or this, but it is IE4+ only: print "<iframe src=\"counter.php\" height=1 width=1></iframe>"; #or you could just create a hidden frame and place your script in thei +r using frames... #Why not just implement your site in PHP?
Re: Make PHP Execute in CGI output
by Anonymous Monk on Dec 02, 2001 at 02:43 UTC
    On a unix box, just change the shebang line: #!user/lib/pathtophp/php //PHP code starts here echo "Wuzzup, world!";
Re: Make PHP Execute in CGI output
by flocto (Pilgrim) on Dec 03, 2001 at 02:46 UTC
    I don't really have any great php experience, but as far as i know there is a binary, called "php" which works much the way the perl executable works. You could pipe all your output through this program (after you're done generating the file) and return it's output to apache as if it was from your script. You could do this by writing a small mod_perl script or implementing it directly into your script(s). Again: I don't know very much about this binary, maybe it doesn't take code from STDIN, maybe it doesn't return to STDOUT, I really don't know very much about this program, but it might work.

    Good luck :)

    GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+
Re: Make PHP Execute in CGI output
by Anonymous Monk on Dec 03, 2001 at 04:17 UTC

Re: Make PHP, yatta yatta....
by bawy (Initiate) on Dec 03, 2001 at 13:29 UTC
    Hey, I posted the "If you are not getting any returned values" post a while ago, I thought of more options:

    You can use the exec() function, it is available in both Perl and PHP so you can crossexecute that way:
    exec("c:/php/php.exe counter.php"); <? exec("c:/perl/perl.exe"); ?>
    You could probably use the LWP module in Perl and pull in the PHP page, although this would be kinda slow considering it is an extra HTTP call for each page.
      I now have 2 installations of php, the server module one and a command line executable one installed to /usr/local/php/. I installed the command line one by leaving off the with-apache configure option.

      So I write my HTML to file then pass it to the PHP executable. Not very efficient but it works. Thank you all for putting me onto this method

      my $html =<<HTML; Content-type: text/html\n\n <?php include('header.php'); ?> <body> <!-- rest of doc --> </body> </html> HTML open PHP, '>phpinput.php'; print PHP $html; close PHP; #-q option to PHP forces it to leave off http headers my $output =eval {`/usr/local/php/bin/php -q phpinput.php`}; print $output unless $@;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128865]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2023-12-10 07:02 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.