By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

setting cookies across subdomains or paths

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
*** 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.
--
-- Álvaro G. Vicario - Burgos, Spain
-- Don't e-mail me your questions, post them to the group
--
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.