Link to home
Start Free TrialLog in
Avatar of matala
matala

asked on

Can a cookie NOT be associated with a page?

Suppose page1.htm creates (nad manages) a cookie by running a series of javascript functions, such as:
cookie()
_cookie_store()
_cookie_load()
_cookie_remove()
which are standard functions for cookies. The cookie stores the username and password of a user. I noticed that the created cookie becomes associated with the page that created it.  How can I access the cookie created by page1.htm from another page, say page2.htm to validate the identity of the user info stored in that cookie?
Avatar of martinag
martinag

It should work.
Are the pages on the same server? Same path?

Martin
What's the source of those functions, by the way?

Martin
Avatar of matala

ASKER

Good question.  They are on the same server, but different path.  I put them both on the same path and they can actually look at the same cookie.  Thanks.
Now does that confirm that a cookie is not associated with a page?
ASKER CERTIFIED SOLUTION
Avatar of jhurst
jhurst

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
If you post the cookie function I can modify it to include the path.

Martin
Avatar of matala

ASKER

Here the code that I am using.  Notice the domain is not passed when I call cookie().

function Cookie(document, name, hours, path, domain, secure)
{
      this.$document = document;
      this.$name = name;
      if (hours) this.$expiration = new Date((new Date()).getTime() + hours*3600000);
        else this.$expiration = null;

      if (path) this.$path = path; else this.$path = null;
      if (domain) this.$domain = domain; else this.$domain = null;
      if (secure) this.$secure = true; else this.$secure = false;
}

function _Cookie_store()
{
       var cookieval = "";
      for(var prop in this)
      {
              // Ignore properties with names that begin with '$' and also methods.
            if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) continue;
            
            if (cookieval != "") cookieval += '&';

            cookieval += prop + ':' + escape(this[prop]);
      }

      var cookie = this.$name + '=' + cookieval;
      if (this.$expiration) cookie += '; expires=' + this.$expiration.toGMTString();
      if (this.$path) cookie += '; path=' + this.$path;
      if (this.$domain) cookie += '; domain=' + this.$domain;
      if (this.$secure) cookie += '; secure';

      this.$document.cookie = cookie;
}


function _Cookie_load()
{
      var allcookies = this.$document.cookie;
      if (allcookies == "") return false;

      var start = allcookies.indexOf(this.$name + '=');
      if (start == -1) return false;   // Cookie not defined for this page.
      
      start += this.$name.length + 1;  // Skip name and equals sign.
      
      var end = allcookies.indexOf(';', start);
      if (end == -1) end = allcookies.length;
      var cookieval = allcookies.substring(start, end);

      var a = cookieval.split('&');    // Break it into array of name/value pairs.
      for(var i=0; i < a.length; i++)  // Break each pair into an array.
            a[i] = a[i].split(':');

      for(var i = 0; i < a.length; i++) this[a[i][0]] = unescape(a[i][1]);

      return true;
}

function _Cookie_remove()
{
      var cookie;
      cookie = this.$name + '=';
      if (this.$path) cookie += '; path=' + this.$path;
      if (this.$domain) cookie += '; domain=' + this.$domain;
      cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

      this.$document.cookie = cookie;
}

//***************************************************

new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

var visitordata = new Cookie(document, "name_password", 1);

Ok, the path is included in the functions. Good...

Replace
  var visitordata = new Cookie(document, "name_password", 1);
with
  var visitordata = new Cookie(document, "name_password", 1, "/");

See that ending "/" I added? That's the path. This means that all documents under that path can access the cookie. You'll probably want to be more specific (espacially if you're at a web hotel or some other kind of server were multiple home pages resides), so give the longest possible path.
For example, if your documents are placed like this:
* /a/b/c/d/e/page1.htm
* /a/b/c/e/f/page2.htm
you could use the path "/a/b/c".

BTW, those functions are pretty nice. Where did you find them?

Martin
matala, are you still there?

Martin
Avatar of matala

ASKER

Thanks martinag; I was away for a while.