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.

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});
# set the cookie in the client browser
$cookie1 = $cgi->cookie(-name => "CGISESSID",
-value => $sid,
-expires => "1h",
-path => "/");


# 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
sub start_body {
my $onload = shift;
return (qq[<BODY $onload><CENTER>Set Session Data</CENTER>]);
sub end_body {
return ( "</BODY></HTML>");
Cheers, Mate!
Quote Reply
Re: [pcrosley] cgi->redirect not setting cookie? - IIS w/ WinNt In reply to

It's a bug in IIS:



Gossamer Threads Inc.
Quote Reply
Re: [Alex] cgi->redirect not setting cookie? - IIS w/ WinNt In reply to
Thanks for that Alex!

I'll see if I can sort it out now.

I presume any "redirect" status gets the same result in IIS (e.g. 301, etc)?
Cheers, Mate!