Setting a 30-minute Cookie

Configuring the expires time for a short-term cookie is tricky.

I routinely use a short-term cookie on admin pages so they automatically expire after half an hour of idle time (the cookie is rewritten on each page access). But setting the expires value for 30 minutes is quite tricky, and it took me a very long time to wrap my head around it. Here is the format for the expires value in a cookie:

Wednesday, 09-Nov-2008 23:12:40 GMT

And you would use WebDNA's [format] context like so:

[format days_to_date %A, %d-%b-%Y][date][/format] [time] GMT


When the webserver reads a cookie, note that it has to see the expiring time in GMT, so you must code in that adjustment if you are not already in GMT. (The server knows what GMT time is even though it displays local time.)

ADJUST the TIME
Using [math], here is how to adjust the time value for 30-mminutes into the future with a 5-hour time zone offset, such as would be the case in New York:

[math time]{[time]}+{05:30:00}[/math]

(05 is the time zone difference and :30 is the 30 minute cookie duration)

ADJUST the DATE
That's all well and good, but you also need to adjust the date. Let's say you got the time adjustment correct, but ignored the date detail. Your half hour cookie is working just fine during the day. The server says it's Tuesday, 2:30 pm and the expiring time is Tuesday 3 pm + 5 hours, or 8 pm GMT. As the day goes on and you hit 6:30 pm, now your expiring time is 7pm + 5 = midnight. But unfortunately, the DAY part of this cookie is still specified as Tuesday (pulled from the server's local time), so the cookie is immediately expired, now having a value of midnight of Tuesday morning, 19 hours ago. And it will continually expire each time it is set, until midnight, when NOW the server's date shows up as Wednesday.

Here is the code to adjust the date value (split into lines for easier reading):

[math]{[date]}
[showif [math]{[time]}+{05:30:00}[/math]<[math]{05:30:00}[/math]]
+{00/01/0000}
[/showif]
[/math]


This compares the cookie's time with 5:30 am. The object is to add a day once cookie time hits midnight (first part of the comparison will equal 0) and up until the point when our server hits midnight (first part of the comparison will equal 05:30:00), at which time the server and GMT are both on the same day. You may be thinking that you should make the comparison using the server offset itself (not the cookie time difference), and make the extra day stop when when server time is midnight (+5 hours) and rolls over to the next day. That's fine on the end of the adjustment period, but the beginning will be messed up. If you compared server time+5 < 5, then if the cookie is set at 18:30, then 23:30 (server+5) would NOT be less than 5 yet, but cookie time (server+5+:30) would be 00:00 - the beginning of the next day. You need to make the server add the day exactly when the COOKIE TIME hits midnight, and continue up until the server's midnight, when the cookie equals 05 + :30.

ARE YOU ON DAYLIGHT SAVINGS TIME?
Okay, now that we've reasoned that out, there's just one more thing. Daylight Savings Time. When daylight savings time kicks in, you have to allow for that too, because GMT does NOT observe DST. Our offset becomes only 4 hours now. It's not too bad for people west of GMT, but will create an impossible situation for people east of the meridian trying to set a 30-minute cookie. Checking to see if the server's clock is on daylight savings will easily do the trick in both the time portion and the day adjustment:

[showif [date %Z]=EDT]-{01:00:00}[/showif]


So our complete code looks like this:

[format days_to_date %A, %d-%b-%Y][math]{[date]}[showif [math]{[time]}+{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math]<[math]{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math]]+{00/01/0000}[/showif][/math][/format] [math time]{[time]}+{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math] GMT


DON'T REINVENT THE WHEEL
Setting a variable or a function to write expire dates for cookies saves you a lot of time. Let's make a variable out of it so we only have to deal with this once! Put this code into its own file and include it at the top of pages using cookies:

[text]cookie30min=[format days_to_date %A, %d-%b-%Y][math]{[date]}[showif [math]{[time]}+{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math]<[math]{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math]]+{00/01/0000}[/showif][/math][/format] [math time]{[time]}+{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math] GMT[/text]


Using the same process, you can make text variables for long term cookies (a day, a month, a year, even a cookie in the past for instant logging off). To be really efficient, you can make functions out of them and use the preparse script to make them Sandbox-wide (or system wide) without having to include anything. A function looks like this:

[function name=cookie30min][format days_to_date %A, %d-%b-%Y][math]{[date]}[showif [math]{[time]}+{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math]<[math]{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math]]+{00/01/0000}[/showif][/math][/format] [math time]{[time]}+{05:30:00}[showif [date %Z]=EDT]-{01:00:00}[/showif][/math] GMT[/function]


If you copy and paste the above examples, make sure you change the time offset and the time zone abbreviation to match your own server. It's a lot of code; make sure you get them all.

Terry Wilson

DOWNLOAD WEBDNA NOW!

Top Articles:

[biotype]

BioType is a behavioral biometrics WebDNA function based on ADGS research and development (from version 8...

Technical Change History

This Technical Change History provides a reverse chronological list of WebDNA changes...

F.A.Q

A compilation of some user's questions...

AWS Raw WebDNA LAMP-Plus WebServer

Amazon Web Services (AWS) README for Machine Image ID...

Tips and Tricks

A list of user-submitted tips ...

WebDNA Libraries

A list of available libraries for WebDNA...

Related Readings:

How old am I?

You want to express an age in years given the dob (mm/dd/yyyy) and today's date...

How to keep the text variable after a function has finished

By default...

Cloning a Record

Often...

Write Once Use Many

Reuse the same code over and over again on your site with the [include] context...

PayPal gateway

...

Paypal IPN

...