PDA

View Full Version : Cookies not being kept after close browser



yankleber
09-12-2014, 05:28 PM
Hey all!

I am having this issue with cookies. As far as I know it is not rocket science so I don't know what can be going wrong here.

I have tested it with Chrome and Firefox. Both browsers are showing the same behavior: the 'cookied' data remains saved as long as keep the browser opened. I can even close the tab where the cookies were saved since the application stays opened. But if I close completely the program the cookies are gone.

I thought that it could be the expire thing and double checked it but got nothing. Here how I am saving the cookies:


function SetCookies() {
var d = new Date().addHours(24);
var n = d.toUTCString();
document.cookie = "name=" + document.getElementById('name').value;
document.cookie = "over_18=" + (document.getElementById('yover').checked ? 'Y' : 'N');
document.cookie = "expires=" + n;
document.cookie = "path=/";
}

Date.prototype.addHours= function(h){
this.setHours(this.getHours()+h);
return this;
}


Do you see something wrong?

Thanks!

:confused:

jscheuer1
09-12-2014, 08:01 PM
SetCookies function is all wrong. Well not all wrong, but mostly wrong. A single cookie needs at least name and value. If you want it to persist, it also needs an expires. If you want to control the path, it also needs a path. You are setting 4 cookies there at a time. the first two are normal expires at the end of session cookies with no path. The last two have no name or value, so are either ignored, or use expires and path respectively as names. Either way, they can't do much. To see how to set javascript cookies, you can look here:

http://www.dynamicdrive.com/forums/entry.php?32-Compact-Cookie-Unit

It's always been a bit tricky, and at that link I've trued to combine as much as possible. So, any further questions, feel free to ask.

yankleber
09-12-2014, 09:32 PM
John, thanks a lot! I am surely going to use that your neat code!

:)

A further small question... I am going to store a BUNCH of data in the cookies. They are simple stuff as name, age, address, etc. Am I correct to suppose that by using obvious cookie names such as 'name' or 'age' they can be easily overwritten by another site that perhaps use the same names? I mean, my info would be more secure if instead I use something like PAMName, PAMAge, etc (PAM is the acronyms of the company I work for)?

Also I have seen that I should keep the total size of my cookies under 4K. Is this a truth or modern browsers have expanded this limit?

Thanks!

jscheuer1
09-12-2014, 11:57 PM
Never hurts to make cookie names as unique as possible. However, if you include path info (the code I offer in my blog post does so automatically), it's less important. But then - what you are trying to avoid is not being overwritten by another script or program on the same domain. If it's your domain and you know you are not setting any conflicting cookies on it, you're fine. But, unless you use no other scripts from other developers that might use those common names . . . Well as I said in the beginning, "Never hurts to make cookie names as unique as possible." For example, scripts on Dynamic Drive that set cookies usually set cookies named:

ddscriptname

or even:

ddscriptnamecookiedescription

where the green and red parts are substituted for by the actual script name and a description of what the cookie does (what kind of info it holds - name, score, etc.) respectively.

yankleber
09-15-2014, 03:59 PM
Cool, John, really awesome! Thank you very much for all your support!

:)