Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple cookie path question 2

Status
Not open for further replies.

spicymango

Programmer
May 25, 2008
119
CA
Hi,

I am reading and setting cooie like the following
Code:
function SetIntroPageCookie() {
 
cookieName = "intropage";
cookieValue = "Yes";
nDays= 365;

var today = new Date();
 var expire = new Date();
 
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();

}


function readIntroPageCookie() {
name=	"intropage";
var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


When I set cookie I do not set path, will it cause and problem?

Secondly I do not set domain what difference will it make, I see that cookie geting set under the domain which I am hitting

 
I always set the path to be '/', unless I need to put path restrictions in place. This minimises any issues with your location when reading or writing the cookie.

As for the domain, you probably don't need to worry unless you need to read/write across multiple domains, because it will default, AFAIK...

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Is it possible to set cookie under multiple domains? I was under the impression you can set the cookie only for the domain you are hitting.

I mean If I have a file which sets the cookie is under domain citybank.com. I can set the cookie for citybank.com, but can i set the cookie for the domain usabank.com?




 
I've never tried cross-domain scripting with completely unrelated domains - I've only ever set cookies for abc.xyz.com that could also be read from def.xyz.com.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
i noticed even though i am not setting path, but in fire fox when i see my cookie for path I see "/" does that mean if you don't set path it by default get set to "/" so there is no need to set path if that what one wants
 
here are some great code i used
Code:
function SetCookie(cookieName, cookieValue, path, domain, secure){
	var expires = new Date();
	expires.setTime(expires.getTime() + 100000000);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
	+ (expires ? '; expires=' + expires.toGMTString() : '')
	+ (path ? '; path=' + path : '/')
	+ (domain ? '; domain=' + domain : '')
	+ (secure ? '; secure' : '');
}
function GetCookie(name){ 
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
} 
function DelCookie(cookieName, cookieValue, path, domain, secure){ 
	var cookieValue="hello";
	var expires = new Date();
	expires.setTime(expires.getTime() - 100000);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
	+ (expires ? '; expires=' + expires.toGMTString() : '')
	+ (path ? '; path=' + path : '/')
	+ (domain ? '; domain=' + domain : '')
	+ (secure ? '; secure' : '');
}

Code:
//use it like following is OK
SetCookie(username,"haha");
//Or
SetCookie("username","haha","/");
//get
GetCookie("username");
//delete
DelCookie("username");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top