We help IT Professionals succeed at work.

Perl Sessions

itnifl asked
See the code below. It is a part of a perl script that is embedded into a html file like this:
<iframe src="cgi-bin/script.pl" Width=90% height=320>

Everytime I reload the page I get a _new_ session id printed. I would assume that I would want to load the same session to access the stored data within and not create a new session every time the page loads?
What do I need to do?

my $cgi=new CGI;
my $currentPagePosition;
$session = new CGI::Session(undef, $cgi, {Directory=>"/tmp"});

if(!$session->param('pagePosition')) {
	$session->param('pagePosition', 0);
} else {
	$currentPagePosition = $session->param('pagePosition');
my $id = $session->id();
print $id;

Open in new window

Watch Question

Where's your cookie and header creation/output?

Based on what you've posted, I'd expect it to generate an "Internal Server Error" since there was no output of the html header prior to the "print $id;" statement.
Please post a short but complete script that demonstrates the problem.
IT Supervisor
Top Expert 2009
Yes - you want to use the header() call from the CGI::Session module in order for the session to work correctly.  Change line 4 in your code from a blank line to:
print $session->header();

Open in new window



Yes, that worked, but now I am getting:
"Content-Type: text/html; charset=ISO-8859-1"
in the html file from the print $session->header(); statement.
Nem SchlechtIT Supervisor
Top Expert 2009

Odd.  It shouldn't.  Can you post more of the HTML code (including the iframe you mentioned above)?