Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Friday May 28, 2004
04:10 PM

Talking to PHP from Perl

[ #18991 ]
I'm looking to do some Perl code that can access PHP code, so that we don't have to rewrite the working PHP code in Perl. It looks like we can pass stuff around via the Apache notes table. Here's what I've done as proof of concept:

package TW::Apache::Play;

use strict;
use warnings;
use Apache::Request;
use Apache::Constants qw( :common );

sub handler {
    my $r = Apache::Request->new( shift );

    my $username = $r->param('username') || "alester";

    $r->send_http_header( "text/html" );

    print "<html><body>";
    print "<h3>Looking up \"$username\" via PHP</h3>";

    my $sub = $r->lookup_uri( "/foo.php" );
    $sub->notes->set( username => $username );
    my $rc = $sub->run();
    my $notes = $sub->notes;

    print "custid=$notes->{custid}<BR>";
    print "contact=$notes->{contact}<BR>";
    print "All done!";
    print "</body></html>";

    return OK;


This mod_perl handler fetches a parm from the URL, and passes it into the notes table for a subrequest. Then, it executes that subrequest, which is the following PHP file:

    $username = apache_note( "username" );
    $cols = sqldo( "select custid,contact from users where username=:u", Array( ":u"=>$username ) );

    apache_note( "custid", $cols["CUSTID"] );
    apache_note( "contact", $cols["CONTACT"] );

The PHP gets the username note from Apache, does a lookup in the database, and then passes back two columns in the notes table. Control is the passed back to the mod_perl handler, which extracts those two columns from the notes table and displays them on the screen.

Of course, in real life, the PHP code will be calls to some hairy business logic that is thoroughly debugged that I don't want to rewrite on the Perl side.

Is there any danger in doing this? As I understand the notes table, they're per-request only, and won't stick around after the request is completed, so I don't have to worry about children polluting each other. Surely there's a performance hit of some kind, but I assume it's only relatively small.

Any words of experience from anyone out there?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.