0

I have a cookie that is being set by this function (page calendar.html):

function bookit(id){
document.cookie='eventid' + "=" + id;
document.location.href ="/sys/account.php";
}

After that the users is redirected to account.php so he can sign in and the id is used on that page and this is what I have on account.php:

function getCookieValue(key)
{
    currentcookie = document.cookie;
    if (currentcookie.length > 0)
    {
        firstidx = currentcookie.indexOf(key + "=");
        if (firstidx != -1)
        {
            firstidx = firstidx + key.length + 1;
            lastidx = currentcookie.indexOf(";",firstidx);
            if (lastidx == -1)
            {
                lastidx = currentcookie.length;
            }
            return unescape(currentcookie.substring(firstidx, lastidx));
        }
    }
    return "";
}

alert(getCookieValue("eventid"));

well the dilemma is : when I refresh calendar.html the alert returns 51 for example which is the value of that event, BUT when i refresh account.php it says: NULL I have tried a jquery cookie plugin, same results so I went back to basic javascript, its driving me insane at this point I dont understand how to pass that value. I figured using cookies would be the right method - but its not working.any suggestions please? I have a javascript debugger my code has no errors.

cppit
  • 4,478
  • 11
  • 43
  • 68
  • 1
    Why are you not passing the value with a GET querystring parameter? – epascarello Apr 02 '13 at 12:23
  • because if they are not signed in ...account.php will redirect them to index.php. then they have to signup, then after that they have to login again.so then I ll have to pass it 4 or 5 times through the url which is not very clean. – cppit Apr 02 '13 at 12:25
  • Probably just an issue of the _path_ of the cookie – if both pages reside under different path, the cookie might not be “visible” between the pages. Solution: Set path explicitly, so that the browser is allowed to serve the cookie for URL paths. – CBroe Apr 02 '13 at 12:25
  • Set cookie with path like document.cookie='eventid' + "=" + id + "; path=/"; so that cookie is visible to all pages – Amit Apr 02 '13 at 12:28
  • @Amit I did that already doesnt work, i tried again just now since you mentioned it and nothing – cppit Apr 02 '13 at 12:30
  • Maybe you need to set the domain on the [cookie](https://developer.mozilla.org/en-US/docs/DOM/document.cookie)? – epascarello Apr 02 '13 at 16:01

0 Answers0