AGE
AGE

Reputation: 3792

How to update a cookie properly?

I am creating and setting a cookie via the code snippet I created below.

When I try to grab the document.cookie to verify that my cookie was created properly, I see a discrepancy.

Allow me to elaborate:

I am using a 50/50 random number generator, generated by the Math.floor(Math.random() * 2) and stored within cookieValue. Therefore, I ensure I always have a 0 or a 1 as my cookie value. This, as well as the rest of the parameters will be part of cookieStr, which will be passed onto the document.cookie in order to create a cookie.

However, in order to verify that this properly occurred, I need to make sure that the current cookie SomeCookieName has the same value as the output of my console.log(cookieStr). Meaning I am properly updating the cookie dynamically.

One quick look at the code below (in the console) and I can tell something is amiss. It always has SomeCookieName of 0, where as the random number generator will output 0 and 1 respectively.

TL;DR:

I believe I am not properly updating the value of the SomeCookieName parameter properly, which should be either 0 or 1 based on the cookieValue random function; but I don't know why this is happening. What am I doing wrong?

var hoursToAdd = 3,
    daysToAdd = 10,
    cookieValue = Math.floor(Math.random() * 2), // Random value 0 or 1
    cookieName = "SomeCookieName",
    cookiePath = "/",
    cookieDomain = "*.somedomainname.com";

setCookie(cookieName, cookieValue, 0, hoursToAdd, cookiePath, cookieDomain);

console.log(document.cookie);

function setCookie(c_name, c_value, c_exdays, c_exhours, c_path, c_domain) {
  var exdate = new Date(),
      cookieStr = "";

  // Setting up the cookie value
  cookieStr = c_name + "=" + escape(c_value) + "; ";

  // Determining the expiration date increase: days or hours
  if (c_exdays) { // Adds days to current date
    exdate.setDate(exdate.getDate() + (c_exdays));
  } 
  else if (c_exhours) { // Adds hours to current date
    exdate.setHours(exdate.getHours() + c_exhours);
  }

  // Setting up the cookie expiration date
  cookieStr += "expires=" + exdate.toUTCString() + "; ";
  
  // Setting up the cookie Path
  if (c_path) {
    cookieStr += "path=" + c_path + "; ";
  }

  // Setting up the cookie Domain
  if (c_domain) {
    cookieStr += "domain=" + c_domain + "; ";
  }
  
  console.log(cookieStr);

  // Creating the cookie 
  document.cookie = cookieStr;
}

EDIT: adding console log output.

The console output I am looking at seems erratic to me. That is because I am expecting the SomeCookieName value should be the same every time. Note there are two console.log look at the snippet above and try it to look at the console output yourself:

SomeCookieName=0; expires=Fri, 29 Jan 2016 23:14:34 GMT; path=/; domain=*.somedomainname.com;

SomeCookieName=0; _ga=GA1.2.988843064.1445444954; BCSI-CS-9cd60cd21b772d48=2

SomeCookieName=1; expires=Fri, 29 Jan 2016 23:14:35 GMT; path=/; domain=*.somedomainname.com;

SomeCookieName=0; _ga=GA1.2.988843064.1445444954; BCSI-CS-9cd60cd21b772d48=2

SomeCookieName=1; expires=Fri, 29 Jan 2016 23:14:36 GMT; path=/; domain=*.somedomainname.com;

SomeCookieName=0; _ga=GA1.2.988843064.1445444954; BCSI-CS-9cd60cd21b772d48=2

SomeCookieName=1; expires=Fri, 29 Jan 2016 23:15:02 GMT; path=/; domain=*.somedomainname.com;

SomeCookieName=0; _ga=GA1.2.988843064.1445444954; BCSI-CS-9cd60cd21b772d48=2

SomeCookieName=1; expires=Fri, 29 Jan 2016 23:15:03 GMT; path=/; domain=*.somedomainname.com;

SomeCookieName=0; _ga=GA1.2.988843064.1445444954; BCSI-CS-9cd60cd21b772d48=2

Upvotes: 2

Views: 2226

Answers (1)

apsillers
apsillers

Reputation: 115940

If you include a domain parameter when setting document.cookie, then in order to read that cookie, the domain must match the domain/subdomain of the page where the script is running.

In this case, you probably set a SomeCookieName=0 with a domain, earlier, while testing. Then, you tried to change it, but you included an invalid domain, so the cookie was not set.

If you make domain match your actual domain, or omit it entirely, the cookie will be set and change as you reset it.

Upvotes: 1

Related Questions