0

I have some code that has been working fine - until I moved the session initialisation into one of my modules. Now the cookie is no longer being set via print->header - and so a new session is created every time I run it. Here's the (heavily pruned/edited) code:

# login.pl

use CGI;
use CGI::Session ( '-ip_match' );
use DBI;

use MyPM qw(&open_db &close_db &getSession); 

use strict;
use warnings;

my $cgi = new CGI;
my $dbh = open_db;
my $session = getSession($cgi, $dbh);   # Call to MyPM.pm
close_db($dbh);

...... code ......

print $session->header(-type => 'application/json');
print $json;

=======================================

# MyPM.pm

use CGI::Session ( '-ip_match' );

our @ISA = qw(Exporter);
our @EXPORT_OK = qw( open_db close_db getSession );

sub getSession {  

  my $cgi = shift;
  my $dbh = shift;

  my $CGICOOKIE = $cgi->cookie('CGISESSID') || 'x';
  my $lng = length($CGICOOKIE);
  if ( $lng != 32 ) print redirect( -URL => $home );

  my $session = CGI::Session->new('driver:MySQL', $cgi, { Handle=>$dbh }) ;
  return $session; 

}

If I change the call to getSession back into this:

my $session = CGI::Session->new('driver:MySQL', $cgi, { Handle=>$dbh });

It works perfectly again and the cookie is set.

Where might I be going wrong?

Upland
  • 691
  • 3
  • 8
  • 18

1 Answers1

0

I realised the problem is when I am checking for the cookie - I am checking for a cookie of 32 characters ... but ignoring the fact that it may be missing. Sorry for my stupidity!

Upland
  • 691
  • 3
  • 8
  • 18