Solved

Can a cookie NOT be associated with a page?

Posted on 1999-01-06
9
155 Views
Last Modified: 2010-04-09
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?
0
Comment
Question by:matala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1841373
It should work.
Are the pages on the same server? Same path?

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1841374
What's the source of those functions, by the way?

Martin
0
 

Author Comment

by:matala
ID: 1841375
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
jhurst earned 50 total points
ID: 1841376
Cookies are associated with domains or sub-domains depending on how they are set.  If all pages are on the same server and the cookie setting code does not specify any sub-domain then they can be shared.  If however the sub-domain is set in the cookie then it is restricted to that.
0
 
LVL 4

Expert Comment

by:martinag
ID: 1841377
If you post the cookie function I can modify it to include the path.

Martin
0
 

Author Comment

by:matala
ID: 1841378
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);

0
 
LVL 4

Expert Comment

by:martinag
ID: 1841379
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
0
 
LVL 4

Expert Comment

by:martinag
ID: 1841380
matala, are you still there?

Martin
0
 

Author Comment

by:matala
ID: 1841381
Thanks martinag; I was away for a while.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question