PDA

View Full Version : Curious cookie naming confusion problem?


peterinwa
04-21-2003, 06:01 AM
I was having a problem with cookies and eventually figured out that I could do exactly this:

1. Read a cookie named stCookie and find it null.
2. Write a cookie named listCookie.
3. Read stCookie again and find it to have the same new value as listCookie.

I simply changed the name of listCookie to listNumCookie and it solved the problem.

Obviously the last eight characters of the cookies that were getting "confused" were both stCookie. But I don't see why this should cause a problem.

Any ideas? Just curious... Peter

P.S.

Sometimes my cookies just seem to get confused and deleting them and starting over solves the problem. It could have to do with my little old '95 PC running Windows '98.

glenngv
04-21-2003, 06:16 AM
it might be the cookie script you are using.
you can verify the cookies stored depending on the browser you use.

peterinwa
04-21-2003, 06:38 AM
Hmm. I've been using the same code for years and never thougt of that:

// Save cookie for 1 year
var X1y=new Date();
X1y.setTime(X1y.getTime()+(1000*60*60*24*365));
function saveCookie1y(name,value,expires){
c=name+"="+escape(value)+"; expires="+expires.toGMTString();
document.cookie=c}

// Get cookie function
function getCookie(name){
var cname=name + "=";
var dc=document.cookie;
if (dc.length > 0){
begin=dc.indexOf(cname);
if (begin != -1){
begin += cname.length;
end=dc.indexOf(";", begin);
if (end == -1) end=dc.length;
return unescape(dc.substring(begin,end))}}
return null}

glenngv
04-21-2003, 08:09 AM
this line in getCookie() function is the cause:

begin=dc.indexOf(cname);

if we follow the 3 steps you enumerated, reading stCookie in step 3 will extract the value of listCookie since the former is a substring (indexOf returns > -1) of the latter.

peterinwa
04-21-2003, 04:11 PM
It's amazing how "bad code" can work for years till a problem shows up! Thanks, Peter