Problem in Set/Get Cookie

S

Sucpraran

Hello:

Below is the Perl/JavaScript code to set and get a cookie in our
applicaion. The problem we are facing is, it works 90% but rest of the
10% we get nothing in getCookie. The browser is set up to accept
cookies(we are 100% that the browser settings are OK). We have users
across the country and we could not find a pattern with browser
type/versions or OS type/versions. In addition sometimes the same user
could login from different PC/Desktop but not from that desktop.

We do not have a clue on what could be wrong. Any suggestions?.

Thanks


<Begin: Perl Code to Set/Get Cookie Cookie Name: CSSession>

sub setCookie
{
my $name = $_[0] ;
my $value = $_[1] ;
my $Time = $_[2] ;
my %MonthNames = (1=>"Jan", 2=>"Feb", 3=>"Mar", 4=>"Apr", 5=>"May",
6=>"Jun", 7=>"Jul", 8=>"Aug", 9=>"Sep", 10=>"Oct", 11=>"Nov",
12=>"Dec") ;
my %Wdays = (0=>"Sunday", 1=>"Monday", 2=>"Tuesday",
3=>"Wednesday", 4=>"Thursday", 5=>"Friday", 6=>"Saturday") ;
my $secure = " secure" ;
my $seconds = time() + ($Time*60) ;
my @GMtime = gmtime($seconds) ;
my $Cyear = $GMtime[5] + 1900 ;
my $Cmonth = $GMtime[4] + 1 ;
my $expiration = sprintf("$Wdays{$GMtime[6]},
%02d-$MonthNames{$Cmonth}-%02d %02d:%02d:%02d GMT", $GMtime[3],
$Cyear, $GMtime[2], $GMtime[1], $GMtime[0]) ;
my $path = "" ;
my $domain = "" ;
my $cookiestr = "Set-Cookie: $name=$value; expires=$expiration" ;
return "$cookiestr" ;
}


sub getCookie
{
my ($GlobalPtr) = @_ ;
my (@rawCookies) = split (/; /,$ENV{'HTTP_COOKIE'});
$GlobalPtr->{'INCOOKIE'} = $ENV{'HTTP_COOKIE'} ;
foreach(@rawCookies)
{
($key, $val) = split (/=/,$_);
$GlobalPtr->{$key} = $val;
}
}
<End: Perl Code to Set/Get Cookie>

<JavaScript function to verify the cookie properties of browser>
<script language="JavaScript1.3">
<!--
function GetSessionID (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getSessionVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function getSessionVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function focusAt(formname, felement){
window.document.forms[formname].elements[felement].focus();
}
function CheckSettings() {
var agt = navigator.userAgent.toLowerCase();
var appname = navigator.appName.toLowerCase() ;
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var SessionID = GetSessionID('CSSession') ;
var clen = 0 ;
__redirectflag__
if(SessionID != null) {
clen = SessionID.length ;
document.LOGINFORM.userID.focus();
}
else {
if(RedirectFlag == 0) {
document.location.href="__nocookieurl__" ;
}
}
}
//-->
</script>
 
G

Gunnar Hjalmarsson

Sucpraran said:
Below is the Perl/JavaScript code to set and get a cookie in our
applicaion. The problem we are facing is, it works 90% but rest of
the 10% we get nothing in getCookie. The browser is set up to
accept cookies(we are 100% that the browser settings are OK). We
have users across the country and we could not find a pattern with
browser type/versions or OS type/versions. In addition sometimes
the same user could login from different PC/Desktop but not from
that desktop.

We do not have a clue on what could be wrong. Any suggestions?.

Well, the best suggestion would be that you ask for help in a suitable
newsgroup. clpmisc is for discussing Perl. ;-)

Anyway, you may want to try 'Sun' instead of 'Sunday', 'Mon' instead
of 'Monday', etc. as the values in %Wdays.
 
T

Tore Aursand

Below is the Perl/JavaScript code to set and get a cookie in our
applicaion.

First of all: Use CGI.pm do to the shitty job of getting/setting the
cookie. It's very simple:

## Get
my $cookie_value = $cgi->cookie( 'cookie_name' );

## Set
my $cookie_value = 'foobar'; # Usually some random string
my $cookie = $cgi->cookie(-name => 'cookie_name',
-value => $cookie_value,
-expires => '+15m');
print $cgi->header(-cookie => $cookie );

Lookup 'perldoc CGI' for more information.
 
G

Gregory Toomey

It was a dark and stormy night, and Sucpraran managed to scribble:
Hello:

Below is the Perl/JavaScript code to set and get a cookie in our
applicaion. The problem we are facing is, it works 90% but rest of the
10% we get nothing in getCookie. The browser is set up to accept
cookies(we are 100% that the browser settings are OK). We have users
across the country and we could not find a pattern with browser
type/versions or OS type/versions.

The 10% of users that dont send a cookie probably have cookies turned off in their browser.

Cookies are a very unreliable way of keeping state information.

gtoomey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top