/    Sign up×
Community /Pin to ProfileBookmark

Cookie creation question

I have created a HTML/JS page that sets a cookie and reads it when the user returns.

No problems on one page … it works great.

My question is this:
I want to use the same cookie creation in a different sub-directory.

Do I need to change the name of the cookie for this duplicated script
or can I leave it the same in the second directory.

Does it read as a different save/restore pattern when in a different subdirectory
or do I need to change the name regardless if in a different directory or not?

How would I test to assure myself that the same cookie name is saving different
information when used in different directories?

Thanks for any thoughts.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinAug 19.2008 — [SIZE="1"]

function setCookie(isName,isValue,daysExpire){

expDate = new Date();

if(daysExpire){ expDate.setTime(expDate.getTime()+(86400000*daysExpire)); }

else{expDate.setTime(expDate.getTime()+24*
60*60*1000*365);}

[COLOR="Red"]// document.cookie = isName+"="+escape(isValue)+";expires="+expDate.toGMTString();

document.cookie = isName+"="+escape(isValue)+";expires="+expDate.toGMTString()+"; path=/";[/COLOR]


}



function getCookie(name) {

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 unescape(c.substring(nameEQ.length,c.length));

}

return false;

}



function deleteCookie(name) {

setCookie(name,"",-1);

}

[/SIZE]


Try swapping the comment on the two red lines. The one with the "path=/" will allow cookies to be read across the directory
Copy linkTweet thisAlerts:
@JMRKERauthorAug 19.2008 — Thanks, I'll give it a try.

But for my clarification:
The one with the "path=/" will allow cookies to be read across the directory[/QUOTE]
My current cookie does contain the path=/,

so it would look at the stored valued in the root directory?

Use of the path=/some_other_dir_path would cause all cookies to be stored in the specified directory (/some_other_dir_path)?

Is that the jist of what happens?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...