SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
Thread: cookie question
-
Apr 3, 2007, 21:43 #1
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
cookie question
Hello, can someone please tell me what this means in javascript?
expires = expires * 1000 * 3600 * 24;
-
Apr 4, 2007, 01:27 #2
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:expires = expires * 1000 * 3600 * 24;
Does that answer your question?- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 4, 2007, 16:59 #3
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not sure but thank you, I'm just trying to be clear on how it works so I can adjust it. Anyone else have any imput on this? Please let me know thanks.
So if I changed it to 48 that wouldn't mess anything up? It would just make it change on 2 days instead of one?
-
Apr 4, 2007, 20:41 #4
- Join Date
- Nov 2002
- Location
- Montréal, Canada
- Posts
- 375
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Instead of changing the number 24, set the variable expires to 2. That will make it expire in two days.
Though it really doesn't matter either way.
-
Apr 4, 2007, 23:58 #5
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 5, 2007, 00:05 #6
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Maybe I could explain it this way:
1000 = 1000 milliseconds in a second.
3600 = 60 * 60 - That would be... 60 seconds in a minute times 60 minutes in an hour.
24 = 24 hours in a day.
Equals: 86400000 milliseconds in a day
Cookie will expire in 86400000 milliseconds- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 5, 2007, 00:08 #7
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
you could also have done it this way:
24 * 60 * 60 * 1000 = 86400000
24 * 60 = 1440 minutes in a day
1440 * 60 = 86400 seconds in a day
86400 * 1000 = 86400000 milliseconds in a day- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 6, 2007, 14:37 #8
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Man, thanks a lot I really appreciate that detail. I tried changing the 24 to 48
but it didn't prolong the cookie. Should I be changing all the other values as well so they will equal 48 too? To hamstu, how would I change the variable to 2 only and how with the script know that means 2 days?
This one line of codeexpires = expires * 1000 * 3600 * 24;
Please get back to me thank you very much.
-
Apr 8, 2007, 01:09 #9
- Join Date
- Apr 2006
- Location
- Germany
- Posts
- 53
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No. "*" means "multiply" just as "+" means "add".
Therefore it doesn't matter if you write 1000*60*60*24 or 86400000
To hamstu, how would I change the variable to 2 only and how with the script know that means 2 days?
And the "2" is a part of this multiplication term.
This one line of code is what I need to adjust because it sets how long a browser cookie is read by the web site that the code is in.to code or not to code ?
that's too much of a question for a signature.
-
Apr 10, 2007, 00:01 #10
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by 1Jen
PHP Code:expires = expires * 1000 * 3600 * 24;
So, do the following, and you'd have control over incrementing the value by days:
PHP Code:expires = 1;
expires = expires * 1000 * 3600 * 24;
PHP Code:expires = 2;
expires = expires * 1000 * 3600 * 24;
PHP Code:expires = 3;
expires = expires * 1000 * 3600 * 24;
As for it not keeping the cookie for that long, there could be a number of reasons for this. The first thing to check would be Tygatur's suggestion. Secondly, you have to make sure that the cookie is actually being set by your script... I've seen many cookie scripts which just don't work. If you're unsure, post your code here and we'll scan it to make sure that it's actually doing what it's supposed to do.- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 14, 2007, 21:47 #11
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks a lot guys, I didn't check on this for a few days but did just now. Thanks for the help that explains it much better. I'll see what happens now. I think that expire by day should help a lot.
-
Apr 18, 2007, 01:35 #12
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well it looks like I need to do something else. I adding the extra expires on 3 days in but it didn't help. Since that didn't work you may as well see the whole function. Maybe you can see how this is reading. How many days is this supposed to expire? You said these are multiplication marks * so how many days expiring is this supposed to be? Thanks very much.
function cookie(name, value, expires, path, domain, secure){
var today = new Date();
if(expires){
expires = expires * 1000 * 3600 * 24;
}
document.cookie = name+'='+escape(value) +
((expires) ? ';expires=' + new Date(today.getTime() + expires).toGMTString() : '') +
((path) ? ';path=' + path : '') +
((domain) ? ';domain=' + domain : '') +
((secure) ? ';secure' : '');
}
-
Apr 18, 2007, 01:50 #13
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Okay... I tested your script, and it worked.
What I did was, I used your function to set the cookie. Right after I set the cookie, I used my personaly cookieLibrary, and alerted the cookie value with my GetCookie Function. Right after I set and alerted the cookie, I removed the set "cookie" function, and alerted the value again. I then changed my date one day forward, and the cookie value still came up. I then reset my date another day ahead, and the value still came up. After setting my date 3 days ahead, the value came back as null. So it works fine for me... there must be something else you're doing wrong.
Maybe you can have a look at my library... I wrote these functions years ago... and I've been using this library ever since. I hardly ever use the expires parameter, since I've set a default expiry date, and a lot of the time, I don't particularly want it to expire... since cookies will be cleared when a user clears his cache and cookies in his browser... either way, they've worked for me.... play around, and maybe you'll find your mistake. I'm not too sure why it doesn't work for you... like I said... it worked fine for me.
Code:function cookie(name, value, expires, path, domain, secure){ var today = new Date(); if(expires){ expires = expires * 1000 * 3600 * 24; } document.cookie = name+'='+escape(value) + ((expires) ? ';expires=' + new Date(today.getTime() + expires).toGMTString() : '') + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ((secure) ? ';secure' : ''); } function SetCookie(name, value) { var expireDate = new Date(); expireDate.setDate(expireDate.getDate() + 40); var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; var expires = (argc > 2) ? argv[2] : expireDate; var path = (argc > 3) ? argv[3] : "/"; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : true; document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == null) ? "; secure" : ""); } 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 getCookieVal(offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function DeleteCookie(name) { var expires = new Date(); expires.setYear(expires.getFullYear() - 1); // This cookie is history var cval = GetCookie(name); document.cookie = name + "=" + cval + "; expires=" + expires.toGMTString(); } //cookie("MyCookie","Test",3); alert(GetCookie("MyCookie"));
- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 19, 2007, 19:06 #14
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh man geeze, I really appreciate that, you did a lot for me. But why would it come back ok on the other days you set but not when you set it to the third? I'll check it out and see what happens and I'll let you know, thanks soooooo much for the help!!!!
-
Apr 20, 2007, 00:06 #15
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No swaet... that's what we're here for init?
It comes back ok within a date range of three days... the expires parameter basically states that, a cookie should be valid for the given range. So if you set expires to 3, it will be valid until the 3rd day, thereafter it will be gone.- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 20, 2007, 00:07 #16
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Something interesting that I did notice during the testing of your script, is that when I set my date 3 days ahead, the value came back as null, and I would have thought that the cookie would have been deleted.... but I then set my date back to normal, and the cookie value came up again. So, my browser never got rid of the cookie... it merely checks if it is still a valid cookie, and if not, it returns null.
- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 20, 2007, 16:52 #17
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You so nice! It's very refeshing. I'll get some things together and send them to you next week for helping me, that may help you too because I very much appreciate your knowledge you have on cookies. I won't have a chance to test your script for some days again.
Your last message could be very important. Don't really understand what you mean. What do you mean by the value came back as null but never got rid of the cookie? How does it look when its null and how does it look when it gets rid of the cookie? If I know those two things it will help me to know what I'm doing.
-
Apr 25, 2007, 06:44 #18
- Join Date
- Jun 2001
- Location
- Cape Town, South Africa
- Posts
- 548
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Okay... firstly, if you haven't set a cookie, and try to get a value from it, it will return null. In other words, say you do a javascript alert on GetCookie("MyCookie"). If you haven't previously set a cookie called "MyCookie", the browser won't have a clue what it is, so alerting something that doesn't exists will display "null" in the alert message box.
Don't worry about my last message... that was just an observation... not important at all. It's like one of those "if you notice this notice, you'll notice that this notice is not worth noticing" ;O)
Anyway... basically, what I was saying is that... when a cookie expires, personally I would imagine that a web browser will delete that cookie. If I delete a previously set cookie, and try to alert it's value again, it returns null, as it would when it hasn't been set at all. But when I changed my date back to the right date during the testing of your script, suddenly, the cookie was visible again. So, the browser never deletes the cookie... it purely works according to the expiry date of the cookie...
As with my DeleteCookie function, you will notice that, all I do is I set the cookie's expiry date to like a year ago... then the browser doesn't recognise the cookie anymore... So it's never actually been "deleted".
I dunno if I'm making much sense, but I can't think of any other way to explain ;p- Pip
---------------------------------------------------------------------------------
Nothing takes the taste out of peanut butter quite like unrequited love.
-
Apr 28, 2007, 23:29 #19
- Join Date
- May 2006
- Posts
- 349
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think I got it, but I still don't understand what you mean by the value of mine came back as null. Did it actually say the word "null" on the page somewhere? Or did you know it was null by another reason you just listed. Thanks.
Bookmarks