I'm having difficulties in setting cookies for a domain and subdomain of that domain. Whilst I cannot divulge the domain name I can however name it "domain.com" and provide code.
I'm trying to achieve the following: visitor comes to domain.com and certain cookies are set. When he reaches a certain page on the subdomain.domain.com those cookies have to be read.
If the user lands directly on the subdomain pages, the cookies get set correctly and everything is ok, but if he lands on the main domain pages no cookies are set. Can you help me?
Again I must reaffirm that the below codes work when subdomain sets the cookies but no cookies are set by the main domain visits (and yes the script is within the main domain pages as well). I have tested with firebug and firecookie.
This is my code on setting the cookies:
function setCookie(c_name,value) {
var now = new Date();
var time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
var c_value=escape(value);
document.cookie=c_name + "=" + c_value + '; path=/;domain=.domain.com';
}
And this is my code on getting the cookie values:
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
return unescape(y);
}
}
}