Gossamer Forum
Home : General : Perl Programming :

cgi->redirect not setting cookie? - IIS w/ WinNt

Quote Reply
cgi->redirect not setting cookie? - IIS w/ WinNt
I am new to Perl, and have looked at many references to try and figure out what I'm doing wrong, to no avail!

I have a site where I use a central Perl script to log page access by a user and then redirect them to the actual page. Part of the redirect is to also set a cookie (CGISESSID) so that I know this is a continuation of an existing session.

First time into the website the cookie is set (I have IE set to prompt for cookies). After that, it never does again and it is seeing each subsequent request as a new session.

Again, I've probably just cocked something up, and the code is listed below.

Any help is greatly appreciated.

-----------------------------------------
#!/usr/bin/perl
use CGI qw(:standard -nph);
use CGI::Session;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI;
use strict;
$CGI::POST_MAX=1024 * 10; # max 10K posts
$CGI::DISABLE_UPLOADS = 1; # no uploads
# Initialise some variables
my $state = "old";
my $cgi = CGI->new;
my $pg = $cgi->param("pg");
my $fullpg = "http://www.gctf.org.nz/" . substr($pg, 3, (length($pg)-3));
my $mysqldsn = 'DBI:mysql:inventory:localhost';
my $db_user_name = 'sid';
my $db_password = 'gctf';
my $dbh;
my $statement;
my ($session, $cookie1, $rc);
# Get existing Session Information
my $sid = $cgi->cookie("CGISESSID");

$dbh = DBI->connect($mysqldsn, $db_user_name, $db_password, {RaiseError => 1}) or die "Couldn't connect to db $mysqldsn: ", DBI->errstr();;
# Now setup to capture the session
if ($sid eq undef) {
$session = new CGI::Session("driver:MySQL", $cgi, {Handle => $dbh});
$sid = $session->id;
$state = "new";
}
else {
$session = new CGI::Session("driver:MySQL", $cgi, {Handle => $dbh});
}
$session->expire("1h");
# set the cookie in the client browser
$cookie1 = $cgi->cookie(-name => "CGISESSID",
-value => $sid,
-expires => "1h",
-path => "/");

$session->flush;

# get the client IP address
my $ipadr = $ENV{REMOTE_ADDR};
my $eref = $cgi->self_url;
my $vhost = $cgi->virtual_host();
my $script = $cgi->script_name();

# Now update the session (in the database) with the current date
if ($state eq "new") {
my $statement = qq[Update sessions set SessionStart = Now(), SessionIp="$ipadr" where Id="$sid"];
$rc = $dbh->do($statement)
or die "Couldn't execute update statement: ", DBI->errstr();
}

if ($pg ne undef) {
# And now tell PageTrack where the user is going
$rc = $dbh->do(qq[Insert into PageTrack (SessionId, PageTimeStamp, GoingTo, SessionIp) values (
"$sid", Now(), "$pg", "$ipadr")])
or die "Couldn't prepare pagetrack insert statement: ", DBI->errstr();
print $cgi->redirect( -cookie => $cookie1,
-location => $fullpg,
-status => 302,
-nph => 1);
}
else {
print $cgi->header( -cookie=>$cookie1 );
my $htmstr = &start_body();
print $htmstr;
print "<br><h2>Where am I supposed to go?</h2>";
$htmstr = &end_body;
print $htmstr;
}
#Clean Up
$dbh->disconnect();
#----------------------------------------------------------
sub start_body {
my $onload = shift;
return (qq[<BODY $onload><CENTER>Set Session Data</CENTER>]);
}
#----------------------------------------------------------
sub end_body {
return ( "</BODY></HTML>");
}
Cheers, Mate!
Subject Author Views Date
Thread cgi->redirect not setting cookie? - IIS w/ WinNt pcrosley 5337 Mar 10, 2005, 3:50 PM
Thread Re: [pcrosley] cgi->redirect not setting cookie? - IIS w/ WinNt
Alex 5230 Mar 10, 2005, 5:41 PM
Post Re: [Alex] cgi->redirect not setting cookie? - IIS w/ WinNt
pcrosley 5216 Mar 10, 2005, 6:15 PM