CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Session fails when working script is included from PHP



Jun 23, 2010, 8:49 PM

Post #1 of 3 (911 views)
Session fails when working script is included from PHP Can't Post


I have a perl script which:

1) checks if the user is logged in
2) if they are logged in, prints the corresponding links
3) otherwise they are logged out, prints the corresponding links

It works absolutely fine if I run the Perl script directly.

However as soon as I run the Perl script from a PHP script using the PHP include() function, it doesn't recognize the user is logged in, as if the session code has failed. Note that both scripts are located in the same directory.

Is there anybody who is able to explain to me why I am having this problem and how I could possibly solve it?

The PHP contains ONLY the following line:

<?php include("") ?>

And here is the Perl:

#! /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 HTML::Template;
use Cwd;
require (getcwd().'/Library.lib');
# Variables
my %data;
# Verify logged in
$data{'Session'} = VerifyLoggedIn();

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();
if ($session->param(-name=>'LoggedIn')) {
$session->expire('LoggedIn', "+10000s");
return $session->param(-name=>'LoggedIn');
else {
return 0;

$data{'eBookLinks'}{'Pass'} = ($data{'Session'}) ? 1 : 0;
# 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
<TMPL_IF eBookLinksPass>
<li class="inner"><a href="">All eBooks</a></li>
<li class="inner"><a href="">Free eBooks</a></li>
<li class="inner"><a href="">My eBooks</a></li>
<li class="inner"><a href="">Options</a></li>
<li class="inner"><a href="">Logout</a></li>
<li class="inner"><a href="">Login</a></li>
<li class="inner"><a href="">Register</a></li>
<li class="inner"><a href="">Forgotten Password</a></li>

Thanks alot,


(This post was edited by Zhris on Jun 23, 2010, 9:02 PM)


Jun 24, 2010, 5:54 AM

Post #2 of 3 (900 views)
Re: [Zhris] Session fails when working script is included from PHP [In reply to] Can't Post

When you say you run it directly, is that from the command line? When you put this location, in your browser, does it give the correct response?


I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Jun 24, 2010, 12:56 PM

Post #3 of 3 (894 views)
Re: [shawnhcorey] Session fails when working script is included from PHP [In reply to] Can't Post


Thank you for replying. When I say I run it directly I mean when I go to the location

I wasn't sure if I would recieve a response so I cross posted this question at Although I didn't recieve a definate solution I was some what able to make it work as I anticipated using the following (but its a very brute method):

exec("/usr/bin/perl; perl", $output);
array_shift($output); array_shift($output); array_shift($output); array_shift($output); array_shift($output);
echo implode($output);

I also noticed the Perl module PHP::Sessions which would allow me to create the session in perl and php, in my perl script, although I have never been able to install modules on my webserver.

Thanks again,



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives