Link to home
Start Free TrialLog in
Avatar of EOLNik
EOLNik

asked on

Cannot find cookies created via Javascript

I am creating cookies using Javascript running in a web page. I am not setting an expiration date. When I read the cookie back using Javascript, I get the value, but when I try to find the cookie manually using Windows Explorer, I cannot find it. I have searched in both C:\Documents and Settings\[XPuser]\Cookies and C:\Documents and Settings\[XPuser]\Local Settings\Temporary Internet Files, but cannot find the cookie. Am I missing something?
SetCookie("User", "XYZ", null, "/");
 
alert(GetCookie("User"));
 
function GetCookie(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 getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
    }
      return null;
    }
 
function SetCookie(name,value,expires,path,secure) {
  document.cookie = name + "=" + escape (value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  //((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
}

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

It should be under the Cookies folder in text file that corresponds to your domain.
Avatar of EOLNik
EOLNik

ASKER

Yes, that's exactly where it should be, but it is not. No file matching the domain or IP address, nor can I find any text matching the cookie name or value if I run a search on the cookies folder. I have tried this on four different machines, and no cookie.
Avatar of EOLNik

ASKER

If you want to try it for yourself, you can paste the below snippet of code into an HTML page, add onload="createCookie();" to the body tag, and run the page. Then try to see if you can find the cookie.
<script type="text/javascript" language="JavaScript">
    function createCookie() {
    SetCookie("User", "XYZ", null, "/");
 
    alert(GetCookie("User"));
 
    function GetCookie(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 getCookieVal (j);
          i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
        }
          return null;
        }
     
    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
          endstr = document.cookie.length;
          return unescape(document.cookie.substring(offset, endstr));
        }
 
    function SetCookie(name,value,expires,path,secure) {
      document.cookie = name + "=" + escape (value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      //((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    }
 
}
    </script>

Open in new window

Try specifying an expiration date.
"If you set no expiration date on a cookie, it expires when the browser closes"
http://msdn.microsoft.com/en-us/library/ms533693(VS.85).aspx
Avatar of EOLNik

ASKER

I will try it that way, but I never actually close the browser, so the above does not apply. And the fact that I can read the cookie value back using JS means that it exists. It is just not anywhere to be found.
I use this cookie viewer. I had the same problem and it was the only way I could find them. http://www.karenware.com/powertools/ptcookie.asp
Avatar of EOLNik

ASKER

Using Karen's Cookie Viewer did not help find my missing cookies, simply because they are not in any of the locations the app searches for them.

The mystery deepens. I have further established the following:

1.) When I run my cookie creation page/script in IE from localhost, a cookie does in fact get created in my cookies folders.
2.) When I run the same thing using Firefox, the cookie does not get created in my cookies folders (but I can read its value using Javascript).
3.) When I run it on a remote web server in either IE or Firefox, the cookie does not get created in my cookies folders, yet I can read their value using Javascript.

So in the last two scenarios I know the cookie exists, it just cannot be found in the file system-anywhere.

I dare anyone to try this for themselves. I have attached two simple HTML files; one that creates the cookie and reads the value, and one that reads the cookie again, deletes it, and then reads its value after deletion (it will be null or empty). Rename the files by removing the ".txt" extension and then run them on your localhost and server while watching your "C:\Documents and Settings\%USER%\Cookies" folder.

I am very curious to see if anyone gets different results and can explain this behavior.
setcookie.htm.txt
delcookie.htm.txt
EOLNik, the cookies are not shared across browsers. If you set a cookie from IE, it will not be read from Firefox; OR if you visit a site with IE and the site sends you a cookie, that cookie is handled by IE, so FF will still not see it. The Cookies folder is for IE only. (Not sure where FF stores its cookies - or any other browser for that matter).

I tried looking in C:\Documents and Settings\username\Local Settings\Temporary Internet Files but my system is not giving access to the folder. I can only see a listing of all it's contents via Tools > Internet Options > [Temporary Internet Files] Settings, but can't access the files. That seems like a likely candidate.
Avatar of EOLNik

ASKER

Thanks for the reply. I am not trying to read cookies in one browser that were created in another--the idea of doing it in different browsers was merely to demonstrate the different behaviors. The fact is, I cannot find cookies anywhere in my file system that were created by running my script anywhere other than on my local webserver (localhost). The fact that they can be read using Javascript (and MUST therefor exist somewhere) is extremely perplexing. And it seems no one can explain where they are stored if they are not anywhere on the computer.

I need to know where these cookies are stored in order to troubleshoot an application's behavior in different environments. Is there anyone who knows how these cookies are stored if they cannot be found on the filesystem?
ASKER CERTIFIED SOLUTION
Avatar of EOLNik
EOLNik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial