multiple cookie problem
Moderator: General Moderators
multiple cookie problem
-
Hi,
I read in multiple cookies with document.cookie.split(";").
I have 10 cookies, all look like this
cookie1=abcd
and contain only [a-Z] and [0-9].
My problem is that I can't read all 10 cookies from time to time. Instead of all 10 cookies document.cookie only returns one cookie - but all 10 are set in the browser.
I can't figure out the problem, but different data length will result in different behaviour. The error is then reproducable to 100percent.
(data length is not over cookie size, about 50-200 chars)
Suggestion or solution anyone?
djot
-
Hi,
I read in multiple cookies with document.cookie.split(";").
I have 10 cookies, all look like this
cookie1=abcd
and contain only [a-Z] and [0-9].
My problem is that I can't read all 10 cookies from time to time. Instead of all 10 cookies document.cookie only returns one cookie - but all 10 are set in the browser.
I can't figure out the problem, but different data length will result in different behaviour. The error is then reproducable to 100percent.
(data length is not over cookie size, about 50-200 chars)
Suggestion or solution anyone?
djot
-
-
Hi,
Here is my code.
This function takes a string, breaks it down to fit in cookie space and sets 10 cookies.
This function regets the cookies (or does not sometimes as mentioned)
content of cookie 1:
code of cookie 2 (which is set but not read by document.cookie)
thx for helping
djot
-
Hi,
Here is my code.
This function takes a string, breaks it down to fit in cookie space and sets 10 cookies.
Code: Select all
function SetCookies(cookiedata)
{
var start = 0;
var maxdata = 3000;
for (i = 1; i <= 10; i++)
{
cookiepart = cookiedata.substr(start,maxdata);
document.cookie = "chatkeks" + i + "=" + cookiepart;
start = start + maxdata;
}
} // function SetCookies()Code: Select all
function GetCookies()
{
if (document.cookie && document.cookie != "")
{
var whole_cookie = unescape(document.cookie);
var each_cookie = whole_cookie.split(";");
//sorting loop
for (i=0; i < 10; i++)
{
if (each_cookieїi].indexOf("chatkeks1") > -1) { var chatkeks1_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks2") > -1) { var chatkeks2_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks3") > -1) { var chatkeks3_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks4") > -1) { var chatkeks4_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks5") > -1) { var chatkeks5_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks6") > -1) { var chatkeks6_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks7") > -1) { var chatkeks7_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks8") > -1) { var chatkeks8_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks9") > -1) { var chatkeks9_data = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeks10") > -1) { var chatkeks10_data = each_cookieїi]; }
} // for
//splitting data and assigning final display variable
// error occurs in data of second cookie (which is not set, undefined error)
var chatkeks1_split = chatkeks1_data.split("="); chatkeks1 = chatkeks1_splitї1];
var chatkeks2_split = chatkeks2_data.split("="); chatkeks2 = chatkeks2_splitї1];
var chatkeks3_split = chatkeks3_data.split("="); chatkeks3 = chatkeks3_splitї1];
var chatkeks4_split = chatkeks4_data.split("="); chatkeks4 = chatkeks4_splitї1];
var chatkeks5_split = chatkeks5_data.split("="); chatkeks5 = chatkeks5_splitї1];
var chatkeks6_split = chatkeks6_data.split("="); chatkeks6 = chatkeks6_splitї1];
var chatkeks7_split = chatkeks7_data.split("="); chatkeks7 = chatkeks7_splitї1];
var chatkeks8_split = chatkeks8_data.split("="); chatkeks8 = chatkeks8_splitї1];
var chatkeks9_split = chatkeks9_data.split("="); chatkeks9 = chatkeks9_splitї1];
var chatkeks10_split = chatkeks10_data.split("="); chatkeks10 = chatkeks10_splitї1];
} // if (document.cookie && document.cookie != "")
} // function GetCookies()content of cookie 1:
Code: Select all
templatebox:::templateboxQQQtemplaterow:::templaterowQQQmessages:::user51qqmessage51qquser52qqmessage52qq ... 241qqmessage241qquser242qqmessage242qCode: Select all
quser243qqmessage243qq ... 300qqmessage300qqdjot
-
-
Hi,
I deleted and inserted cookies for 2h hours already to fix that problem; the insert function sets always (at least empty) 10 cookies; I also tried to add dummy data to all of the 10 cookies, what is no problem.
Only reading the cookies results in just one returned cookie from time to time, even if 10 are set and fully loaded with data. But since I do nothing special than just read plain text with document.cookies, I can't figure out where the problem is.
djot
-
Hi,
I deleted and inserted cookies for 2h hours already to fix that problem; the insert function sets always (at least empty) 10 cookies; I also tried to add dummy data to all of the 10 cookies, what is no problem.
Only reading the cookies results in just one returned cookie from time to time, even if 10 are set and fully loaded with data. But since I do nothing special than just read plain text with document.cookies, I can't figure out where the problem is.
djot
-
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
why is this being done with Javascript and not PHP ?
anyway, regarding your code:
it works for me, with one exception: chatkeks1 ends up equal to chatkeks10.
which, actually makes sense.. as will match "chatkeks1" and "chatkeks10".
maybe you should count from zero?
anyway, regarding your code:
it works for me, with one exception: chatkeks1 ends up equal to chatkeks10.
which, actually makes sense.. as
Code: Select all
if (each_cookieїi].indexOf("chatkeks1") > -1) { var chatkeks1_data = each_cookieїi]; }maybe you should count from zero?
-
Hi,
thx feyd, did you win the posting contest :) ?
djot
-
Hi,
I hate cookies and Javascript, and avoided both the last 8 years. But these 10 cookies are to be a client side cache and therefor have to be parsed with JS.why is this being done with Javascript and not PHP ?
This is another error I get crazy from. I did count from 1-9, 0-10 and a-h already, all throw at least one bug: cookie1==cookie10 or cookie10 causes error being undefinded (even if it's not). And I count from zero with for (i=0; i < 10; i++)!??it works for me, with one exception: chatkeks1 ends up equal to chatkeks10
maybe you should count from zero?
thx feyd, did you win the posting contest :) ?
djot
-
-
Hi,
I think my problem is in
Seems like the cookies do not end with ";" from time to time.
But how can this occur? I thought the browser sets the semicolon!?
Ok, got at least the second error: string "chatcookie1" ist recognized in "chatcookie10" also for sure.
djot
-
Hi,
I think my problem is in
Code: Select all
var each_cookie = whole_cookie.split(";");But how can this occur? I thought the browser sets the semicolon!?
Ok, got at least the second error: string "chatcookie1" ist recognized in "chatcookie10" also for sure.
djot
-
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
if your data you are putting into the cookies has a semicolon unescaped, that may be the problem. In a quick test, that did truncate the data passed for that record.. I'd suggest using escape on all the data as it goes in. (This will also avoid a problem if you have an = in there too.) You'll need to move your unescape to later in the function. You can do the variable setting in a loop as well.
-
Hi,
I rewrite all 10 cookies each time my page is meta_refreshed.
All ten get written correctly, the data has only letters, no special chars, and they are escaped, even if it would make no sense on only letters [a-z]
Unfortunately, when the page is refreshed the fith time, all cookies get written, but the second cookie gets an "last visited 1970:" while cookie 1, and 3-10 get an last vistited ("last visited 2004 10-10").
When page refrehes the 6th time, only cookies 1 and 3-10 can be read. Cookie 2 returns "undefined" (even if it is still in the browsers' cookie with all it's data).
djot
-
Hi,
I rewrite all 10 cookies each time my page is meta_refreshed.
All ten get written correctly, the data has only letters, no special chars, and they are escaped, even if it would make no sense on only letters [a-z]
Unfortunately, when the page is refreshed the fith time, all cookies get written, but the second cookie gets an "last visited 1970:" while cookie 1, and 3-10 get an last vistited ("last visited 2004 10-10").
When page refrehes the 6th time, only cookies 1 and 3-10 can be read. Cookie 2 returns "undefined" (even if it is still in the browsers' cookie with all it's data).
djot
-
-
Hi,
I had this error in Opera, but IE does the same.
Here is the full code, please first try it unchanged, then alter $random_data_amount .
chat.js
djot
-
Hi,
I had this error in Opera, but IE does the same.
Here is the full code, please first try it unchanged, then alter $random_data_amount .
Code: Select all
<?php
$id = $_GETї'id'];
$action = $_GETї'action'];
// if this lowered to e.g. 5, the script refreshes more times
$random_data_amount = 50;
if (empty($action))
{
$action = "frame";
$js_template = "
<script language="JavaScript">
<!--
newtemplatebox = "templatebox";
newtemplaterow = "templaterow";
-->
</script>
";
}
if ($action == "msg")
{
$msg = "";
for($i=1; $i<=$random_data_amount; $i++)
{
$line = $id + $i;
$msg .= "user".$line."qqmessage".$line."qq";
}
echo "newmessages = "".$msg."";";
}
if ($action == "frame")
{
$js_messageid = "
<script language="JavaScript">
<!--
messageid = "".$id."";
-->
</script>
";
} // if ($action == "frame")
if ($action == "frame")
{
$line = $id + $random_data_amount;
$chat ="
<html>
<head>
<meta http-equiv="refresh" content="4; url=./chat.php?action=frame&id=".$line."">
".$js_template.$js_messageid."
<script language="JavaScript" src="./chat.php?action=msg&id=".$id.""></script>
<script language="JavaScript" src="./chat.js"></script>
</head>
<body>
<script language="JavaScript">
<!--
document.write(chat);
-->
</script>
</body>
</html>
";
echo $chat;
} // if ($action == "frame")
?>Code: Select all
var config_cookies_datasize = 3999;
var config_cookies_amount = 10;
var config_cookies_names = ї"a","b","c","d","e","f","g","h","i","j"];
function ReplaceUserMsg(text)
{
replaced = text;
rExp = /user/gi;
replaced = replaced.replace(rExp, '')
rExp = /message/gi;
replaced = replaced.replace(rExp, '')
rExp = /qq/gi;
replaced = replaced.replace(rExp, '')
rExp = /ї0-9]/gi;
replaced = replaced.replace(rExp, '')
return replaced;
}
function DrawChat(box,row,msg)
{
drawchat = box + row + msg;
return drawchat;
}
function ReplaceSpecialSigns(text)
{
rExp = /\{\{SIGN_EQUAL\}\}/gi;
replaced = text.replace(rExp, "=")
return replaced;
}
function ReplaceUnwantedSigns(text)
{
rExp = /\=/gi;
replaced = text.replace(rExp, '{{SIGN_EQUAL}}')
return replaced;
}
function SetCookies(cookiedata)
{
if (messageid == 200) { alert("This is the last time, all cookies are set correctly. Next time cookies are get/read, the second cookie is undefinded.\nfunction SetCookies()"); }
var start = 0;
var expiration_date = new Date("January 1, 3000");
expiration_date = expiration_date.toGMTString();
var now = new Date();
now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365)
for (i = 0; i < config_cookies_amount; i++)
{
cookiepart = cookiedata.substr(start,config_cookies_datasize);
cookiestring = "chatkeks" + config_cookies_namesїi] + "=" + cookiepart + ";expires=" + now;
if (messageid == 200) { alert("function SetCookies()\n\n" + cookiestring); }
document.cookie = cookiestring;
start = start + config_cookies_datasize;
}
} // function SetCookies()
function GetCookies()
{
if (document.cookie && document.cookie != "")
{
var whole_cookie = unescape(document.cookie);
var each_cookie = whole_cookie.split(";");
var chatkeks_data = ї];
//sorting loop
for (i=0; i < config_cookies_amount; i++)
{
if (each_cookieїi].indexOf("chatkeksa") > -1) { chatkeks_dataї'a'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksb") > -1) { chatkeks_dataї'b'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksc") > -1) { chatkeks_dataї'c'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksd") > -1) { chatkeks_dataї'd'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkekse") > -1) { chatkeks_dataї'e'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksf") > -1) { chatkeks_dataї'f'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksg") > -1) { chatkeks_dataї'g'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksh") > -1) { chatkeks_dataї'h'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksi") > -1) { chatkeks_dataї'i'] = each_cookieїi]; }
if (each_cookieїi].indexOf("chatkeksj") > -1) { chatkeks_dataї'j'] = each_cookieїi]; }
} // for
//splitting data and assigning final display variable
var chatkeks_split = ї];
var chatkeks = ї];
for (i=0; i < config_cookies_amount; i++)
{
cookieindex = config_cookies_namesїi];
chatkeks_splitїcookieindex] = chatkeks_dataїcookieindex].split("=");
if ( typeof(chatkeks_splitїcookieindex]ї1]) != "undefined" && chatkeks_splitїcookieindex]ї1] != "")
{
chatkeksїcookieindex] = chatkeks_splitїcookieindex]ї1];
}
}
var cookiedata = "";
for (i=0; i < config_cookies_amount; i++)
{
cookieindex = config_cookies_namesїi];
if ( typeof(chatkeksїcookieindex]) != "undefined" && chatkeksїcookieindex] != "")
{
cookiedata += chatkeksїcookieindex];
}
}
var cookie_parts = cookiedata.split("QQQ");
var templatebox_data = "";
var templaterow_data = "";
var messages_data = "";
for (i = 0; i < cookie_parts.length; i++)
{
if (cookie_partsїi].indexOf("templatebox") > -1) { templatebox_data = cookie_partsїi]; }
if (cookie_partsїi].indexOf("templaterow") > -1) { templaterow_data = cookie_partsїi]; }
if (cookie_partsїi].indexOf("messages") > -1) { messages_data = cookie_partsїi]; }
} // for
//splitting data and assigning final display variable
var templatebox_split = templatebox_data.split(":::"); oldtemplatebox = templatebox_splitї1];
var templaterow_split = templaterow_data.split(":::"); oldtemplaterow = templaterow_splitї1];
var messages_split = messages_data.split(":::"); oldmessages = messages_splitї1];
} // if (document.cookie && document.cookie != "")
} // function GetCookies()
function JsChat()
{
var cookiedata = "";
var messages = "";
GetCookies();
if ( typeof(newtemplatebox) != "undefined" )
{
templatebox = newtemplatebox;
templaterow = newtemplaterow;
newtemplatebox = ReplaceUnwantedSigns(newtemplatebox);
newtemplaterow = ReplaceUnwantedSigns(newtemplaterow);
cookie_newtemplatebox = newtemplatebox + "QQQ";
cookie_newtemplaterow = newtemplaterow + "QQQ";
cookiedata += "templatebox:::" + escape(cookie_newtemplatebox) + "templaterow:::" + escape(cookie_newtemplaterow);
}
else
{
if ( typeof(oldtemplatebox) == "undefined" )
{
oldtemplatebox = "";
oldtemplaterow = "";
}
else
{
templatebox = oldtemplatebox;
templaterow = oldtemplaterow;
}
cookie_oldtemplatebox = oldtemplatebox + "QQQ";
cookie_oldtemplaterow = oldtemplaterow + "QQQ";
cookiedata += "templatebox:::" + escape(cookie_oldtemplatebox) + "templaterow:::" + escape(cookie_oldtemplaterow);
}
cookiedata += "messages:::";
if ( typeof(oldmessages) != "undefined" )
{
messages += oldmessages;
cookiedata += escape(oldmessages);
}
else
{
//alert("");
}
if ( typeof(newmessages) != "undefined" )
{
messages += newmessages;
newmessages = ReplaceUnwantedSigns(newmessages);
cookiedata += escape(newmessages);
}
else
{
//alert("");
}
SetCookies(cookiedata);
chat = DrawChat(templatebox,templaterow,messages);
return chat;
}
var chat = "";
chat = JsChat();djot
-
-
Hi,
>this doesn't happen at all on my machine. The content of your cookies >doesn't look like it's just [a-zA-Z0-9]
But it is. I would be glad if you would find a character causing all this.
>This is a client-side cache of what? It looks like php classes.. which is a >REAL bad idea to store as a cookie.
This is a client side cache for a chat script. I have to pass a little template and the message data, then the messages are created on client side with JS. Advantage of this is that I only have to pass the new messages because the old ones are cached in the cookies.
djot
-
Hi,
>this doesn't happen at all on my machine. The content of your cookies >doesn't look like it's just [a-zA-Z0-9]
But it is. I would be glad if you would find a character causing all this.
>This is a client-side cache of what? It looks like php classes.. which is a >REAL bad idea to store as a cookie.
This is a client side cache for a chat script. I have to pass a little template and the message data, then the messages are created on client side with JS. Advantage of this is that I only have to pass the new messages because the old ones are cached in the cookies.
djot
-
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
but it isn't. ':::' appearing in your cookies is not [a-zA-Z0-9]..>this doesn't happen at all on my machine. The content of your cookies >doesn't look like it's just [a-zA-Z0-9]
But it is. I would be glad if you would find a character causing all this.
I think there's a better solution out there that requires less to no JS..