setting cookies across subdomains or paths

J

Jose Olivas

I am setting a cookie on a subdomain:
http://store1.mydomain.com

Then the store takes me to a shopping cart that is at:
http://shopping.mydomain.com

Somewhere the following code (taken and modified from The JavaScript Source)
is broken between domains, because when I click my "store" link that should
read the cookie and send me to store1.mydomain.com or storeN.mydomain.com I
get the default template store.

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->

<!-- Begin
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
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 SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var favorite = GetCookie('store');

if (favorite != null) {
switch (favorite) {
case 'store1' : url = 'http://store1.mydomain.com/';
break;
case 'store2' : url = 'http://store2.mydomain.com/';
break;
case 'store3' : url ='http://store3.mydomain.com/';
break;
case 'storeN' : url = 'http://storeN.mydomain.com/';
break;
}
window.location.href = url;
}
// End -->
</script>
</HEAD>

The cookie is set by a javascript "onload command" Like I said I have it
working on a server with no sub domain.

Any help would be truly appreciated.

Jose Olivas
 
A

Alvaro G Vicario

*** Jose Olivas wrote/escribió (Mon, 14 Mar 2005 23:30:18 -0700):
I am setting a cookie on a subdomain:
http://store1.mydomain.com

Then the store takes me to a shopping cart that is at:
http://

Somewhere the following code (taken and modified from The JavaScript Source)
is broken between domains

I couldn't find the line where you set the cookie value. Anyway, there're a
couple of things about cookies that must be taken into account:

1) Cookies are per-domain settings: they're only sent by browser to pages
in the domain they're attached to. If you need a cookie to be valid in more
than one domain then you need to set more than one cookie, one per domain.

2) Many browsers are configured to discard cookies for third-party domains.
So if you set a cookie at store1.mydomain.com for shopping.mydomain.com
they cookie will most likely be lost.

If you have access to a server-side language you may consider using
sessions or databases.
 
D

Dr John Stockton

JRS: In article <ZhvZd.37256$Im.30367@okepread01>, dated Mon, 14 Mar
2005 23:30:18, seen in Jose Olivas
I am setting a cookie on a subdomain:
http://store1.mydomain.com

Then the store takes me to a shopping cart that is at:
http://shopping.mydomain.com

Somewhere the following code (taken and modified from The JavaScript Source)
is broken between domains,

Don't know.

<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->

That is what URLs are for.
<!-- Begin
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

exp.setDate(exp.getDate() + expDays);
is more rational. It will also give 30 calendar days rather than 30 UTC
days.

function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

Why not use new Date(0) and omit the following line?
Why not explicitly set expires= a past date such as 2000 Jan 1?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top