Perl CGI with PHP login using PHP::Interpreter
From Dev411: The Code Wiki
| Table of contents |
Overview
There is often a desire to combine Perl and PHP in the same web application, usually having one rely on the other's authentication and session management capabilities. This demo shows how a Perl CGI script can access a PHP login include file using PHP::Interpreter.
This demo covers the following topics:
- Instantiating a PHP interpreter from Perl
- Passing parameters to populate PHP's $_POST autoglobal array
- Reassigning the PHP output handler so Perl can retrieve PHP print output
- Including a PHP include file
- Calling a PHP function
- Reading a PHP global variable
- Accessing PHP output via Perl
This demo includes two files:
- a simple PHP login include file that: (a) has a global variable, $logged_in, that is set to true or false; (b) the login() function that checks to see if the form POST parameters 'username' and 'password' are valid; and (c) a displayLogin() function that prints an HTML login form. For the purposes of this demo the correct username is 'demouser' and the password is 'demopass'. The displayLogin() from uses the Perl script as it's action since the Perl script is the only script providing HTML output in this demo.
- a simple Perl CGI script that: (a) instantiates the PHP::Interpreter with the username and password HTTP POST variables; (b) calls the PHP login() function; (c) checks the PHP global variable $logged_in; and (d) depending on the value of $logged_in either displays a logged in message or executes and displays the output of the PHP displayLogin() form.
Prerequisites
To run this demo you will need the following:
- Perl (with the standard library CGI.pm module)
- PHP
- PHP::Interpreter (http://search.cpan.org/~gschloss/PHP-Interpreter-1.0.1/lib/PHP/Interpreter.pm)
This demo has been tested with Perl 5.8.7, PHP 5.1.2 and PHP::Interpreter 1.0.1.
PHP include file
This file can be located anywhere, however, the Perl file will need to know the location to include it.
For this simple demo, the displayLogin() form includes an action to the Perl demo script. The action is /cgi-bin/perl_php_login.pl. Change this if you save the Perl script in a different location.
<?php
$logged_in = false;
function login() {
global $logged_in;
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == 'demouser' && $password == 'demopass') {
$logged_in = true;
} else {
$logged_in = false;
}
}
function displayLogin() {
print ('
<form action="/cgi-bin/perl_php_login.pl" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value=" Login " />
</form>
');
}
?>
Perl CGI Script
The Perl script needs to know the location of the PHP include file. This is referenced in the line with the call to $php->include() below. The PHP file location is /home/demo/perl_php_login/php_login.php. Change this if you save the PHP file in a different location.
#!/usr/local/bin/perl -wT
use strict;
use CGI;
use PHP::Interpreter;
# Instatiate CGI.pm and print header as well as opening html
my $q = CGI->new;
print $q->header,$q->start_html;
# Instantiate PHP::Interpreter
my $php = PHP::Interpreter->new({
POST => {
'username' => $q->param('username')||'',
'password' => $q->param('password')||''
}
});
# Reassign PHP output handler so we can access it from Perl
my $php_output;
$php->set_output_handler( \$php_output );
# Include the PHP login include file
$php->include( '/home/demo/perl_php_login/php_login.php' );
# Call the PHP login function
$php->login;
# Check the PHP $logged_in global variable
if ( $php->eval( 'return $logged_in;' ) ) {
print "Thank you for logging in.\n";
} else {
print "You are not logged in.\n";
# Call PHP displayLogin function
$php->displayLogin;
# Print the PHP output
print $php->get_output;
}
print $q->end_html;
Categories: Perl | PHP | Web Development
