Page 1 of 3

multiple cookie problem

Posted: Sat Oct 09, 2004 10:36 pm
by 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
-

Posted: Sat Oct 09, 2004 10:41 pm
by feyd
post your code.

Posted: Sat Oct 09, 2004 10:58 pm
by 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++)
  &#123;
      cookiepart = cookiedata.substr(start,maxdata);
      document.cookie = "chatkeks" + i + "=" + cookiepart;
      start = start + maxdata;
  &#125;
&#125; // function SetCookies()
This function regets the cookies (or does not sometimes as mentioned)

Code: Select all

function GetCookies()
&#123;
  if (document.cookie && document.cookie != "")
  &#123;
      var whole_cookie = unescape(document.cookie);
      var each_cookie  = whole_cookie.split(";");

      //sorting loop
      for (i=0; i < 10; i++)
      &#123;
          if (each_cookie&#1111;i].indexOf("chatkeks1") > -1)  &#123; var chatkeks1_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks2") > -1)  &#123; var chatkeks2_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks3") > -1)  &#123; var chatkeks3_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks4") > -1)  &#123; var chatkeks4_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks5") > -1)  &#123; var chatkeks5_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks6") > -1)  &#123; var chatkeks6_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks7") > -1)  &#123; var chatkeks7_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks8") > -1)  &#123; var chatkeks8_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks9") > -1)  &#123; var chatkeks9_data  = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeks10") > -1) &#123; var chatkeks10_data = each_cookie&#1111;i]; &#125;
      &#125;  // 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&#1111;1];
      var chatkeks2_split  = chatkeks2_data.split("=");  chatkeks2  = chatkeks2_split&#1111;1];
      var chatkeks3_split  = chatkeks3_data.split("=");  chatkeks3  = chatkeks3_split&#1111;1];
      var chatkeks4_split  = chatkeks4_data.split("=");  chatkeks4  = chatkeks4_split&#1111;1];
      var chatkeks5_split  = chatkeks5_data.split("=");  chatkeks5  = chatkeks5_split&#1111;1];
      var chatkeks6_split  = chatkeks6_data.split("=");  chatkeks6  = chatkeks6_split&#1111;1];
      var chatkeks7_split  = chatkeks7_data.split("=");  chatkeks7  = chatkeks7_split&#1111;1];
      var chatkeks8_split  = chatkeks8_data.split("=");  chatkeks8  = chatkeks8_split&#1111;1];
      var chatkeks9_split  = chatkeks9_data.split("=");  chatkeks9  = chatkeks9_split&#1111;1];
      var chatkeks10_split = chatkeks10_data.split("="); chatkeks10 = chatkeks10_split&#1111;1];

  &#125; // if (document.cookie && document.cookie != "")

&#125; // function GetCookies()

content of cookie 1:

Code: Select all

templatebox:::templateboxQQQtemplaterow:::templaterowQQQmessages:::user51qqmessage51qquser52qqmessage52qq ... 241qqmessage241qquser242qqmessage242q
code of cookie 2 (which is set but not read by document.cookie)

Code: Select all

quser243qqmessage243qq ... 300qqmessage300qq
thx for helping

djot
-

Posted: Sat Oct 09, 2004 11:12 pm
by feyd
sure looks like you only set 1 cookie.

try the following:
  1. create a string to fill all at least 2 cookies.
  2. send said string to the SetCookies function.
  3. document.write out document.cookie when it returns.
if only 1 cookie is in there, you have your answer.

Posted: Sat Oct 09, 2004 11:37 pm
by 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
-

Posted: Sat Oct 09, 2004 11:40 pm
by feyd
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

Code: Select all

if (each_cookie&#1111;i].indexOf("chatkeks1") > -1)  &#123; var chatkeks1_data  = each_cookie&#1111;i]; &#125;
will match "chatkeks1" and "chatkeks10".

maybe you should count from zero?

Posted: Sun Oct 10, 2004 4:41 am
by djot
-

Hi,
why is this being done with Javascript and not PHP ?
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.
it works for me, with one exception: chatkeks1 ends up equal to chatkeks10
maybe you should count from zero?
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++)!??


thx feyd, did you win the posting contest :) ?

djot
-

Posted: Sun Oct 10, 2004 4:52 am
by djot
-
Hi,

I think my problem is in

Code: Select all

var each_cookie  = whole_cookie.split(";");
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
-

Posted: Sun Oct 10, 2004 9:52 am
by feyd
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.

Posted: Sun Oct 10, 2004 10:15 am
by 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
-

Posted: Sun Oct 10, 2004 10:22 am
by feyd
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]

This is a client-side cache of what? It looks like php classes.. which is a REAL bad idea to store as a cookie.

Posted: Sun Oct 10, 2004 10:35 am
by 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

&lt;?php
  $id     = $_GET&#1111;'id'];
  $action = $_GET&#1111;'action'];

  // if this lowered to e.g. 5, the script refreshes more times
  $random_data_amount = 50;



  if (empty($action))
  {
      $action = "frame";
      $js_template = "
&lt;script language="JavaScript"&gt;
&lt;!--
  newtemplatebox = "templatebox";
  newtemplaterow = "templaterow";
--&gt;
&lt;/script&gt;
";
  }

  if ($action == "msg")
  {
      $msg = "";
      for($i=1; $i&lt;=$random_data_amount; $i++)
      {
          $line = $id + $i;
          $msg .= "user".$line."qqmessage".$line."qq";
      }
      echo "newmessages = "".$msg."";";
  }

  if ($action == "frame")
  {

      $js_messageid = "
&lt;script language="JavaScript"&gt;
&lt;!--
  messageid      = "".$id."";
--&gt;
&lt;/script&gt;
";
  } // if ($action == "frame")


  if ($action == "frame")
  {
      $line = $id + $random_data_amount;

      $chat ="
&lt;html&gt;
&lt;head&gt;

&lt;meta http-equiv="refresh" content="4; url=./chat.php?action=frame&amp;id=".$line.""&gt;

".$js_template.$js_messageid."

&lt;script language="JavaScript" src="./chat.php?action=msg&amp;id=".$id.""&gt;&lt;/script&gt;
&lt;script language="JavaScript" src="./chat.js"&gt;&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;script language="JavaScript"&gt;
&lt;!--
document.write(chat);
--&gt;
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
";

      echo $chat;
  } // if ($action == "frame")

?&gt;
chat.js

Code: Select all

var config_cookies_datasize = 3999;
var config_cookies_amount   = 10;
var config_cookies_names    = &#1111;"a","b","c","d","e","f","g","h","i","j"];

function ReplaceUserMsg(text)
&#123;
  replaced = text;
  rExp = /user/gi;
  replaced = replaced.replace(rExp, '')
  rExp = /message/gi;
  replaced = replaced.replace(rExp, '')
  rExp = /qq/gi;
  replaced = replaced.replace(rExp, '')
  rExp = /&#1111;0-9]/gi;
  replaced = replaced.replace(rExp, '')
  return replaced;
&#125;

function DrawChat(box,row,msg)
&#123;
  drawchat = box + row + msg;
  return drawchat;
&#125;

function ReplaceSpecialSigns(text)
&#123;
  rExp = /\&#123;\&#123;SIGN_EQUAL\&#125;\&#125;/gi;
  replaced = text.replace(rExp, "=")
  return replaced;
&#125;

function ReplaceUnwantedSigns(text)
&#123;
  rExp = /\=/gi;
  replaced = text.replace(rExp, '&#123;&#123;SIGN_EQUAL&#125;&#125;')
  return replaced;
&#125;

function SetCookies(cookiedata)
&#123;
  if (messageid == 200) &#123; alert("This is the last time, all cookies are set correctly. Next time cookies are get/read, the second cookie is undefinded.\nfunction SetCookies()"); &#125;

  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 &lt; config_cookies_amount; i++)
  &#123;
      cookiepart    = cookiedata.substr(start,config_cookies_datasize);
      cookiestring  = "chatkeks" + config_cookies_names&#1111;i] + "=" + cookiepart + ";expires=" + now;

      if (messageid == 200) &#123; alert("function SetCookies()\n\n" + cookiestring); &#125;
      document.cookie = cookiestring;
      start = start + config_cookies_datasize;
  &#125;
&#125; // function SetCookies()

function GetCookies()
&#123;
  if (document.cookie &amp;&amp; document.cookie != "")
  &#123;

      var whole_cookie = unescape(document.cookie);

      var each_cookie  = whole_cookie.split(";");
      var chatkeks_data = &#1111;];

      //sorting loop
      for (i=0; i &lt; config_cookies_amount; i++)
      &#123;
          if (each_cookie&#1111;i].indexOf("chatkeksa") &gt; -1) &#123; chatkeks_data&#1111;'a'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksb") &gt; -1) &#123; chatkeks_data&#1111;'b'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksc") &gt; -1) &#123; chatkeks_data&#1111;'c'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksd") &gt; -1) &#123; chatkeks_data&#1111;'d'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkekse") &gt; -1) &#123; chatkeks_data&#1111;'e'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksf") &gt; -1) &#123; chatkeks_data&#1111;'f'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksg") &gt; -1) &#123; chatkeks_data&#1111;'g'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksh") &gt; -1) &#123; chatkeks_data&#1111;'h'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksi") &gt; -1) &#123; chatkeks_data&#1111;'i'] = each_cookie&#1111;i]; &#125;
          if (each_cookie&#1111;i].indexOf("chatkeksj") &gt; -1) &#123; chatkeks_data&#1111;'j'] = each_cookie&#1111;i]; &#125;
      &#125;  // for


      //splitting data and assigning final display variable
      var chatkeks_split = &#1111;];
      var chatkeks       = &#1111;];

      for (i=0; i &lt; config_cookies_amount; i++)
      &#123;
          cookieindex = config_cookies_names&#1111;i];
          chatkeks_split&#1111;cookieindex] = chatkeks_data&#1111;cookieindex].split("=");
          
          if ( typeof(chatkeks_split&#1111;cookieindex]&#1111;1]) != "undefined" &amp;&amp; chatkeks_split&#1111;cookieindex]&#1111;1] != "")
          &#123;
              chatkeks&#1111;cookieindex] = chatkeks_split&#1111;cookieindex]&#1111;1];
          &#125;
      &#125;


      var cookiedata = "";

      for (i=0; i &lt; config_cookies_amount; i++)
      &#123;
          cookieindex = config_cookies_names&#1111;i];
          if ( typeof(chatkeks&#1111;cookieindex]) != "undefined" &amp;&amp; chatkeks&#1111;cookieindex] != "")
          &#123;
              cookiedata += chatkeks&#1111;cookieindex];
          &#125;
      &#125;


      var cookie_parts = cookiedata.split("QQQ");

      var templatebox_data = "";
      var templaterow_data = "";
      var messages_data    = "";

      for (i = 0; i &lt; cookie_parts.length; i++)
      &#123;
          if (cookie_parts&#1111;i].indexOf("templatebox") &gt; -1) &#123; templatebox_data = cookie_parts&#1111;i]; &#125;
          if (cookie_parts&#1111;i].indexOf("templaterow") &gt; -1) &#123; templaterow_data = cookie_parts&#1111;i]; &#125;
          if (cookie_parts&#1111;i].indexOf("messages") &gt; -1)    &#123; messages_data    = cookie_parts&#1111;i]; &#125;
      &#125;  // for
    
      //splitting data and assigning final display variable
      var templatebox_split = templatebox_data.split(":::"); oldtemplatebox = templatebox_split&#1111;1];
      var templaterow_split = templaterow_data.split(":::"); oldtemplaterow = templaterow_split&#1111;1];
      var messages_split    = messages_data.split(":::");    oldmessages    = messages_split&#1111;1];

      
  &#125; // if (document.cookie &amp;&amp; document.cookie != "")

&#125; // function GetCookies()

function JsChat()
&#123;
  var cookiedata = "";
  var messages   = "";

  GetCookies();

  if ( typeof(newtemplatebox) != "undefined" )
  &#123;
      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);
  &#125;
  else
  &#123;
      if ( typeof(oldtemplatebox) == "undefined" )
      &#123;
      	  oldtemplatebox = "";
      	  oldtemplaterow = "";
      &#125;
      else
      &#123;
      	  templatebox = oldtemplatebox;
          templaterow = oldtemplaterow;
      &#125;
      cookie_oldtemplatebox = oldtemplatebox + "QQQ";
      cookie_oldtemplaterow = oldtemplaterow + "QQQ";
      cookiedata += "templatebox:::" + escape(cookie_oldtemplatebox) + "templaterow:::" + escape(cookie_oldtemplaterow);
  &#125;


  cookiedata += "messages:::";

  if ( typeof(oldmessages) != "undefined" )
  &#123;
      messages    += oldmessages;
      cookiedata  += escape(oldmessages);
  &#125;
  else
  &#123;
      //alert("");
  &#125;


  if ( typeof(newmessages) != "undefined" )
  &#123;
      messages    += newmessages;
      newmessages  = ReplaceUnwantedSigns(newmessages);
      cookiedata  += escape(newmessages);
  &#125;
  else
  &#123;
      //alert("");
  &#125;


  SetCookies(cookiedata);

  chat = DrawChat(templatebox,templaterow,messages);

  return chat;
&#125;

var chat = "";
chat = JsChat();

djot
-

Posted: Sun Oct 10, 2004 10:39 am
by 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
-

Posted: Sun Oct 10, 2004 10:50 am
by feyd
>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.
but it isn't. ':::' appearing in your cookies is not [a-zA-Z0-9]..


I think there's a better solution out there that requires less to no JS..

Posted: Sun Oct 10, 2004 11:03 am
by djot
-

I did retest the files shown above, after removing all ":::" and stuff according to template*

so only "user1qqmessage1..." is cookie data, and still the problem exists.

Why the cookies get written correctly 5 refreshes, and the 6th one fails?

djot
-