Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session lost when a Perl script is included in a PHP script 1

Status
Not open for further replies.

Zhris

Programmer
Aug 5, 2008
254
GB
Hey,

Wasn't entirely sure which forum this 1 should belong to.

I have a PHP script, which includes a Perl script that checks if the user is logged in, and prints the appropriate links.

If I run the PHP script when the user is logged in, the "logged out links" are displayed. If I run the Perl script directly when the user is logged in, the "logged in links" are displayed (expected result).

Any suggestion as to the reason why the Perl session variables are not found if the Perl script is included in a PHP script, but works fine when the Perl script is ran directly? Here is the PHP code leading up the the inclusion of the Perl script:

Code:
<?php	
	$current_url = '[URL unfurl="true"]http://'.$_SERVER[/URL]['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
	$dirname = dirname($current_url);
	// title and dirname fix when header is printed using LWP::Simple in a Perl script.
	if (isset($_GET['Title'])) {
		$title = $_GET['Title'];
		$dirname .= '/..';
	}
	echo '<?xml version="1.0" encoding="ISO-8859-1"?>'.PHP_EOL;
?>
<!--<!DOCTYPE html PUBLIC &amp;amp;quot;-//W3C//DTD XHTML 1.0 Strict//EN&amp;amp;quot; &amp;amp;quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&amp;amp;quot;>-->[/URL]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Title > <?php echo $title ?></title>

<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="NAC" content="GWKV4 RK6LD" />

<link rel="stylesheet" type="text/css" href="Include/Styles.css" title="" />
<!--[if lt IE 7]><style type=&amp;amp;quot;text/css&amp;amp;quot;>#left-col{width:200px;}#right-col{width:200px;}</style><![endif]-->
</head>
<body>
<noscript><p>Your browser does not support javascript or javascript has been disabled. As a result, particular features of this website may not work correctly.</p></noscript>
<div id="wrapper">
	<div id="header">
		<div id="left-logo">
		</div>
		<div id="right-title">
			<h1>Title .co.uk</h1>
		</div>
	</div>
	<div id="main">
		<div id="left-col">
			<div id="left-links">
				<ul>
					<li class="top <?php if (preg_match('/^A range/', $title)) { echo 'active'; } ?>"><a href="Home.php">Home ></a></li>
				</ul>
				<ul>
					<li class="top <?php if (preg_match('/^eBooks/', $title)) { echo 'active'; } ?>"><a href="eBooks.pl?View=All">eBooks ></a></li>
					[red]<?php $include_url = $dirname.'/Include/eBook-Links.pl'; include($include_url); ?>[/red]

Thanks alot,

Chris
 
what does the perl script look like?

and are you expecting the php variables to be available to the perl script? if so which variables? the GET and POST etc superglobals will not be available because you are calling perl as if you were simply opening a text file. not executing it via a command line, nor via a webserver. If you want to execute the perl script you will need to use exec or similar. probably best to capture the output in php and echo it if that is what is required.

or are you expecting the perl variables to be available to php? in which case I think you would have to output the variables as php script from the perl script and then eval the output from the perl script.
 
Hey,

Thank you for your reply,

The Perl script uses the session module to check if the user is logged in, printing the appropriate links. There are no variables being passed to either script. I only want to include the output of the perl script. I'm starting to think this issue is due to the header information been declared as HTML before the session script is ran.

Thank you,

Chris
 
I'm unsure this is of any use but the Perl script is as follows:

Code:
#! /usr/bin/perl
use strict;
use CGI ':standard';
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session qw/-ip-match/;
$CGI::POST_MAX = 2000000;
use Digest::MD5 qw/md5_hex/;
use DBI;
use LWP::Simple;
use HTML::Template;
#
use Cwd;
require (getcwd().'/Library.lib');
#####
# Variables
my %data;
#####
# Verify logged in
$data{'Session'} = VerifyLoggedIn();
$data{'eBookLinks'}{'Pass'} = ($data{'Session'}) ? 1 : 0;
use Data::Dumper; print Dumper($data{'Session'});
#####
# Build template
my $template = HTML::Template->new(type => 'filehandle', source => *DATA);
$template->param ( eBookLinksPass => $data{'eBookLinks'}{'Pass'} );
#####
# Print
#print "Content-type: text/html\n\n";
print $template->output;
#####
# Template data
__DATA__
<TMPL_IF eBookLinksPass>
	<li class="inner"><a href="eBooks.pl?View=All">All eBooks</a></li>
	<li class="inner"><a href="eBooks.pl?View=Free">Free eBooks</a></li>
	<li class="inner"><a href="eBooks.pl?View=My">My eBooks</a></li>
	<li class="inner"><a href="Options.pl">Options</a></li>
	<li class="inner"><a href="Login.pl?Logout=true">Logout</a></li>
<TMPL_ELSE>
	<li class="inner"><a href="Login.pl">Login</a></li>
	<li class="inner"><a href="Register.pl">Register</a></li>
	<li class="inner"><a href="Forgotten-Password.pl">Forgotten Password</a></li>
</TMPL_IF>

And the VerifyLoggedIn function:

Code:
sub VerifyLoggedIn{
	my $session = CGI::Session->load() or die CGI::Session->errstr;	
	if (($session->is_expired) || ($session->is_empty)) {
		$session = new CGI::Session();
	}
	print $session->header();
	$session->flush();
	#
	if ($session->param(-name=>'LoggedIn')) {
		$session->expire('LoggedIn', "+10000s");
		$session->flush();
		return $session->param(-name=>'LoggedIn');
	}
	else {
		return 0;
	}
}

Chris
 
Hello,

Not to pressure anyone to suggest a solution, but i'm really stuck here. If I can't find a solution I will probably have to convert all my Perl scripts into PHP to make it more compatible.

Any help at all will be much appreciated.

Thanks,

Chris
 
you need to exec the perl script.

Code:
exec ($include_url, $output, $return);

the output from the perl script will be in $output as an array of lines. you may want to implode the array before using it.

However, I am still concerned that this will not work for you as it will be the php script that is running the perl script, not the webserver. will the same session data be available? I doubt it but know very little about perl.

you may have, instead, to do something like this:

Code:
ob_start();
include '[URL unfurl="true"]http://url[/URL] of perl script';
$contents = ob_get_contents();
ob_end_clean();
//results will now be in $contents

but even then I am not sure this will work the way you expect as the cookies from the users browser are not being sent to the perl script.

but recoding this to php would be the work of five minutes

Code:
session_start();
<?php
define('TIMEOUT', 3600); //60 minutes
session_start();
if (isset($_GET['Logout']) && $_GET['Logout'] == true) logout();
if (!verifyLoggedIn()){
	logout();
}

function verifyLoggedIn(){
	if (!isset($_SESSION['loggedIn'])) return false;
	if (!isset($_SESSION['lastVisit'])) return false;
	if ($_SESSION['lastVisit'] + TIMEOUT < time()) return false;
	updateSession();
	return true;
}

function updateSession(){
	$_SESSION['lastVisit'] = time();
}

function logOut(){
	$_SESSION = array();
	$params = session_get_cookie_params();
	setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
	session_destroy();
	header('');//redirect to login page
}

?>

you will need to deal with the cases where a user is trying to login using his credentials.

I do not know, then, how you make this interact with your other perl scripts.
 
Thank you very much for your input,

I have experimented with exec and have managed to get what I wanted using the following:

Code:
<?php
exec("/usr/bin/perl; perl Include/eBook-Links.pl", $output);
array_shift($output);
array_shift($output);
array_shift($output);
$output = implode($output);
echo $output;
?>

The reason I shift the array 3 times, is to remove the lines printed when I set the session in the header (as PHP has already produced a header). I'm not sure if this is a further problem as i'm assuming it means that perl is not able to set the session properly, even though it seems to be working.

Chris

 
I'm still pretty confident that you will not get the anticipated response as perl will be trying to set the session with your php/web process as the client. so session cookies etc are unlikely to be registered with the actual client's browser. perhaps you could proxy them? or perhaps call your php script from your perl script instead of the other way around?
 
The website structure is slightly overcomplicated, the PHP header I posted is called from another perl script so you can imagine the overall structure looking like:

Code:
-Main perl script
     -Include PHP header
          -Include Perl links script
     -Print statements from main perl script
     -Include PHP footer

You were right, after some testing I didn't recieve the anticipate result once I ran "main perl script".

I have decided my best solution would be to convert all my Perl scripts into PHP to keep it simple (and so that I can keep Header/Footer in PHP), although I am nowhere near as experienced in PHP than I am in Perl, which was the fundametal reason for doing it the way I did in the first place.:

Code:
-Main PHP script
     -Include PHP Header (no longer a need for a links script)
     -Print statements from main PHP script
     -Include PHP Footer

Unfortunately I am unable to install Perl modules to my web server, as I found a module which creates PHP sessions from Perl (PHP::Session), which may have been perfect. I will also look into your proxy suggestion.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top