Display cookies on any web page regardless of subdirectory

I am using the script below to cookie name and email address and then display their values on another page which is located in a different subdirectory. The script only seems to display the cookied values *if* the display page is in the same dierctory as the pae where the name and email address were first cookied.

Would an expert kindly revise the code below so that I can display the cookied values on any page in any subdirectory on my domain?

//  Cookie Library -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
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 to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


/* Calling examples
	var expdate = new Date ();
	FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
	SetCookie ("ccpath", "http://www.hidaho.com/colorcenter/", expdate);
	SetCookie ("ccname", "hIdaho Design ColorCenter", expdate);
	SetCookie ("tempvar", "This is a temporary cookie.");
	SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
	SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
	SetCookie ("goner", "This cookie must die!");
	document.write (document.cookie + "<br>");
	DeleteCookie ("goner");
	document.write (document.cookie + "<br>");
	document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
	document.write ("ccname = " + GetCookie("ccname") + "<br>");
	document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
*/

Open in new window

ehop66Asked:
Who is Participating?
 
Dave BaldwinFixer of ProblemsCommented:
I think you are misunderstanding what you need to do.  That code contains functions that will be run when you call them with a separate line of javascript on your page.  Somewhere later on your page, you should have code like below.  I'd suggest you do not change the code in the functions above but just use them as shown.


<script type="text/javascript">
SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/",".whiterockwebdesign.net");
</script>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Look at line 173.

SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");

Use that format so the cookie can be read anywhere on your domain.
0
 
ehop66Author Commented:
Hmm ...

Line 173 currently reads as

SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");

so I was assuming the cookie would work across any directory or subdierctory on my domain www.whiterockwebdesign.net, but it does not.

I set the cookie in the subdirectory http://www.whiterockwebdesign.net/register/somepage.htm

I can then view the cookied data in http://www.whiterockwebdesign.net/register/anothepage.htm

but I cannot view the cookied data in

http://www.whiterockwebdesign.net/specs/somepage.htm

Do I need to change Line 173 to something like:

SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",whiterockwebdesign.net,"/");
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dave BaldwinFixer of ProblemsCommented:
Since this is the order: function SetCookie (name,value,expires,path,domain,secure)

try:

SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/",".whiterockwebdesign.net");

Notice the '.' in front of the domain name.  That is supposed to match any URL that ends with that like "www.whiterockwebdesign.net" or "sales.whiterockwebdesign.net".
0
 
ehop66Author Commented:
Thanks for your help, Dave :-)

I think I have the general concept down, but not sure (after playin with the script for 2 hours!) where to insert the code you suggested:

SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/",".whiterockwebdesign.net");

Do I need to replace a line of code with the above code you suggested? Or do I have to insert the line of code you suggested someplace?

Also, lines 120-121 suggest I shouldn't have to define the domain because the cookie was created and is being called from the same domain, no?

//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.

Open in new window


Confused :-|

Erik
0
 
Dave BaldwinFixer of ProblemsCommented:
No, you should define the domain with the '.' if you want it to be recognized with different prefixes like 'www'.  You don't need to replace the line in the code above, you need to use that format to call the cookie setting routine on your pages.
0
 
ehop66Author Commented:
I did this which di not seem to help:

function SetCookie (name,value,expires,path,domain,secure) {
var domain = ".whiterockwebdesign.net";
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");      
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.