Solved

Can a cookie NOT be associated with a page?

Posted on 1999-01-06
9
151 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

803 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