PDA

View Full Version : Cookies work ... half the time


agent186
08-10-2006, 09:10 PM
Hi everyone,
I am trying to write a user tracking program, and I'm having some issues with the cookies. I am running FastCgi on a Linux box with Apache 2.0. Here is a synopsis of what my script does.

A JavaScript file is included on every page of the site using this line of code.

<script type="text/javascript" language="JavaScript" src="http://www.mysite.com/collect.js"></script>


The javascript file collects some data about the users computer and performs document.write to call the Perl script.

document.write("<img src='http://www.mysite.com/scripts/collect.cgi?userid="+userid+"&referer="+
referer+"&currentURL="+currentURL+"&visitid="+visitid+"&updated="+updated+"&type="+status+
"' border=0 height=1 width=1>");



The Perl script then performs some various functions and ultimatley writes the cookie using this code.

print LOG "New cookie added \n";
my ($UserID, $VisitID, $App) = @_;
my $cgiCkie = new CGI::Fast;
my $fut_time=gmtime(time()+365*24*3600)." GMT"; # Add 12 months (365 days)

# write new cookie
my $cookie = $cgiCkie->cookie(-name=>'userid',-value=>$UserID,-expires=>$fut_time,-path=>'/');
my $cookie2 = $cgiCkie->cookie(-name=>'visitid',-value=>$VisitID,-expires=>$fut_time,-path=>'/');
my $cookie3 = $cgiCkie->cookie(-name=>'updated',-value=>time(),-expires=>$fut_time,-path=>'/');
my $cookie4 = $cgiCkie->cookie(-name=>'appstarted',-value=>$App,-expires=>$fut_time,-path=>'/');
print $cgiCkie->header(-cookie=>[$cookie, $cookie2, $cookie3, $cookie4]);



Everything works fine in my script ... every other time. The first time the site is hit, it runs, the second time it doesn't, the third time it does, so on and so on. I have confirmed that the cookies are indead the issue. If I comment them out, everything works fine 100% of the time. It may also be worth noting that the Perl script also performs the following action at the beginnning of the script.

print $q->header(-type=>'image/gif',-expires=>'Sat, 29 Jul 2010 11:11:11 GMT');
printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",71,73,70,56,57,97,1,0,1,0,128,255,0,192,192,192,0,0,0,33,249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,6 8,1,0,59);

This means that the header is being written twice. However, I did combine this two together so that it only called the header function once per script call, and it behaved the same way.

I think it might have something to do with Fast::CGI. Unfourtunalty, I'm stuck using it because the rest of the site uses it. I have tried at least five differant ways of setting the cookies and they either don't work at all or work half the time.

Anyone have any idea why this would only work half the time?