Click to See Complete Forum and Search --> : Using cookies with a dynamically changing date
steve_0
12-20-2003, 12:48 AM
This sort of code is what I want to avoid for my cookies:
my $date="Thurdsay, 25-DEC-03 12:00:00 GMT";
How can I replace my $date with a dynamic date?
In the documentation I've read there is a way of adding
dates in the format of +30d or similar to add 30 days in the
future for expiration. Where do I state this?
my $date=+30d; --Is that correct?
Phil Karras
12-26-2003, 03:03 PM
A better way might be to first read the date, or get the date, from your form that the client is using. Then change it as needed to add 30 days to it. (You can do this with JavaScript behind the scenes.)
I can't remember but I think you simply add the +30d to the end of the date string with a space.
In JavaScript I used:
expireDate = new Date;
expireDate.setMonth(expireDate.getMonth()+1);
to set the expiration one month from the time the client used the program.
Now you could send the expireDate as a hidden field to your Perl program. Check to be sure the date format is correct for Perl before just using it.
Hope that helps.
Jeff Mott
12-26-2003, 10:52 PM
my $date=+30d; --Is that correct?Put it in quotes to make it a string then it will be. :) my $date = '+30d';A better way might be to first read the date, or get the date, from your form that the client is using. Then change it as needed to add 30 days to it. (You can do this with JavaScript behind the scenes.)We probably don't want to be relying on client-scripting at all for this.I can't remember but I think you simply add the +30d to the end of the date string with a spaceNope. It's either a string of the form "Wdy, DD-Mon-YYYY HH:MM:SS GMT" or a relative date, which is a positive or negative offset number, and a character representing the units (days, months, minutes, and so on). But the date string can not be both or it is invalid.
steve_0
12-28-2003, 12:13 AM
Another way of doing this without JavaScript code, which could
be dangerous for this exercise is to use the gmtime() function.
I discovered this when searching for an answer to my problem.
This snippet will use the reserved word time (actual clock time)
and add 1 year to it (in seconds) and convert it into
Greenwich Mean Time as required for a cookie in Perl.
Thanks for your help.
my $date = time + 365 * 86400;
my $expire = gmtime($date);
Phil Karras
12-28-2003, 04:00 PM
OK understood, however, the reason I suggested getting the client's date/time is that if the client's machine is way off your actual date/time may have already expired or it may expire 100 years from now.
Getting the client's date/time insures the extension is 30 days as required. (Assuming the client allows JavaScript to run.)
Anyway, glad you found the method needed, thanks for passing it on.