PDA

View Full Version : too many cookies being baked


habib
10-14-2002, 11:54 AM
I am trying to use cookies on my site that will show product prices in different currencies, and different types of products available depending on the country of residence selected. The cookie script is in an external.js file and is called on every webpage.

So far...

I have baked the cookie (sniffs.. "summats burning" - burnt a few!) and been able to retrieve it. It sort of works but having a few problems. I only want to create one cookie at any time on any part of the site. So no matter how the user gets to the site they will only have one cookie and this cookie will be retrieved when ever they access the site again and from any page of the site. I know this can be achieved if all the pages are under one directory (i.e wwwroot), but my pages are in different directories under root.

The problem....

When a page under a different directory is accessed a new cookie is created. So instead of one cookie, I have 5 cookies. The problem this is causing is if a user selects US for their country in the products page (prices are shown in US$, but when they click on 'Buy this product' and the 'buy.htm' page is under a different directory the prices are shown in default currency and not US$.

Here is the script I am using:

var expiration = new Date();
// Heinle's function for retrieving a cookie.
function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
var cookie_value;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
alert('getCookie');
if (Country.country.selectedIndex ==0) {
return 'EU'; }
else
if (Country.country.selectedIndex ==1) {
return 'SA'; }
else
if (Country.country.selectedIndex ==3) {
return 'US'; }
else {
return 'UK'; }
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

Anyone any ideas on how I can just create one cookie and use this one cookie throughout the whole site?

Thanks in advance
HR

brothercake
10-14-2002, 02:36 PM
If you change

((path == null) ? "" : "; path=" + path)

to

path = "/"


Then it will set the cookie at the root level instead of the folder level. If all your cookies have the same name, this should have the effect you want.

habib
10-15-2002, 01:15 PM
Hey, that's so kool!

Thanks
:thumbsup:
HR