Cookie writing confusion

C

Charly Walter

Hello,

Please help.

I keep data in several session variables, one of which is a complex
object tied to a custom DLL I wrote. Everything seems to be running
fine until I introduced a system to remember certain data to cookies.

At some point in my application the following code is executed

WriteCookie(key, value, new Date(((new Date()).getTime() +
(365*86400000))), "/")

Almost immediately after, the session variable holding the complex
object no longer exists (vartype of object = 0). I commented out that
line in the app and the session variable continues to exist.

I am not sure how the session variable and my writing of the cookie is
inter-related. Can someone please explain to me what might be
happening, why and how I can deal with it ?

Thank you,
Charly
 
K

kaeli

At some point in my application the following code is executed

WriteCookie(key, value, new Date(((new Date()).getTime() +
(365*86400000))), "/")

Sessions often store a unique ID on the client computer as a cookie
(some use URL rewriting), which the server uses to look up data with
each request. I suspect a problem with the interaction between the
session cookie and the one you are writing.
What exactly does WriteCookie look like? Got code?


--
 
C

Charly Walter

Thank you for your reply.

I presumed as much since I do see a stub of something when I open the
cookie in a text editor.

The cookie writing code is as follows and is nothing out of the
ordinary, I think:

function WriteCookie(sName, sValue, dExpires, sPath, sDomain, bSecure) {
var sCookie;
var sError;

sCookie = sError = "";


if(sError.length == 0){
sCookie += escape(sName) + "=";
sCookie += escape(sValue);
if(dExpires)sCookie += "; expires=" + dExpires.toGMTString();
if(sPath) sCookie += "; path=" + sPath;
if(sDomain) sCookie += "; domain=" + sDomain;
if(bSecure) sCookie += "; secure=" + bSecure.toString();

document.cookie = sCookie;

if(sValue != GetCookie(sName)) sError = sError + "Write failure/n";
}
return sError;
}

Regards,
Charly
 
K

kaeli

Thank you for your reply.

I presumed as much since I do see a stub of something when I open the
cookie in a text editor.

What name are you calling it?
Have you tried calling it something else?
The cookie writing code is as follows and is nothing out of the
ordinary, I think:

function WriteCookie(sName, sValue, dExpires, sPath, sDomain, bSecure) {
var sCookie;
var sError;

sCookie = sError = "";


if(sError.length == 0){

This will always be true, since you just set it to the empty string,
above. What's the point?
sCookie += escape(sName) + "=";
sCookie += escape(sValue);
if(dExpires)sCookie += "; expires=" + dExpires.toGMTString();

Is this a copy/paste error? There may need to be a space after that
close parens.
if(sPath) sCookie += "; path=" + sPath;
if(sDomain) sCookie += "; domain=" + sDomain;
if(bSecure) sCookie += "; secure=" + bSecure.toString();

document.cookie = sCookie;

Whoops. You just wiped out the previous value of document.cookie. Bad
kitty.

document.cookie += sCookie.
That was probably your problem.

--
--
~kaeli~
Jesus saves, Allah protects, and Cthulhu thinks you'd make
a nice sandwich.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
M

Michael Winter

enlightened us with...
[snip]
if(dExpires)sCookie += "; expires=" + dExpires.toGMTString();

Is this a copy/paste error? There may need to be a space after that
close parens.

That shouldn't matter. It would be more readable, though.

This is actually incorrect. The "secure" field is a boolean in itself. If
it is present, the cookie should be made secure. If it is absent, the
cookie can be stored unaltered. Change it to

if( bSecure ) sCookie += '; secure';

From the OP:

WriteCookie(
key,
value,
new Date(((new Date()).getTime() + (365*86400000))),
"/" );

It's probably better to take advantage of Date()'s ability to roll over
automatically:

var nextYear = new Date();
nextYear.setFullYear( nextYear.getFullYear() + 1 );

WriteCookie(
key,
value,
nextYear,
"/" );
 
C

Charly Walter

Thank you Kaeli and Michael !!

I was thinking of cookie arcania and my problem seemed to be application
of syntax.

Of course, your suggestions did the trick. And I did learn enough to
actually remember some.

Thanks again,
Charly
 

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

Similar Threads

how to delete a cookie? 3
Cookie Problems 1
Closure scope confusion 14
Once Per Session Cookie 4
Need help with this script 4
ipy %run noob confusion 4
Adding dynamic cookie variable from php 0
Cookie encryption? 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top