Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Modifying cookie expire date

by mellin (Scribe)
on Jan 03, 2005 at 21:24 UTC ( [id://419060]=perlquestion: print w/replies, xml ) Need Help??

mellin has asked for the wisdom of the Perl Monks concerning the following question:

This is pretty much related to CGI-stuff, but then again, i'm making it with Perl, so guess that justifies writing this here :)

In standard cookie the expiry-date format is like this: "Sat, 20 Feb 2010 01:31:52 GMT". I would need to create a script that updates this timestamp with each login with 1 hour addon, so the expiry date is allways 1 hour ahead the time user last received a cookie from this script. This way i try to ensure that the problem with shared computers don't come up with login. At least it's controlled in some way, if user forgets to logout.

My question is, how should i test that timestamp and make sure of that 1 hour addon to be made correctly? Should i just test every segment of it, like if hours are now 23, make sure next is 0 and so on? I obviously have to take in consideration leap year as well. I'm sure there's nothing but tons of already made modules for this kind of date manipulation, but i cannot install those to this server, so i need to write my own handler.

I am aware how to split and read that timestamp with regex, so no problem there.

Replies are listed 'Best First'.
Re: Modifying cookie expire date
by brian_d_foy (Abbot) on Jan 03, 2005 at 22:59 UTC

    How are you making your cookies? The CGI.pm module that comes with Perl lets you specify a time offset with the cookie. You don't need to worry about dates and times. Indeed, the example in CGI.pm uses a cookie set one hour into the future (that's the '+1h' thing).

    $cookie = $query->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h', -path=>'/cgi-bin/database', -domain=>'.capricorn.org', -secure=>1);

    Update the cookie each time you need to and let CGI.pm think about the rest.

    However, you can still use any module you like: just because you can't install them on the server doesn't mean you can't use them locally to test your stuff. To make sure the cookie is working like you want it to, you could use LWP or WWW::Mechanize to test it. You don't need to use those from the server (unless there is some weird policy in place).

    --
    brian d foy <bdfoy@cpan.org>
Re: Modifying cookie expire date
by Roy Johnson (Monsignor) on Jan 03, 2005 at 23:11 UTC
    The module Time::Local is distributed with core. If you can safely assume GMT, and that the date format is consistent, this example should be about all you need.
    my $tstr = 'Sat, 20 Feb 2010 01:31:52 GMT'; my %months; @months{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = 0..11; use Time::Local; my @parts = split(/\W+/, $tstr); print join(', ', @parts), "\n"; # Get internal time representation from formatted parts # sec, min, hour, day, mon, year my $newtime = timegm(@parts[6,5,4,1], $months{$parts[2]}, $parts[3]-19 +00); # Add an hour, in seconds $newtime += 60*60; # Reformat into new time string $tstr = gmtime($newtime); # Comma after day, and GMT tag $tstr =~ s/(?=\s)/,/; $tstr .= ' GMT'; print "New time is <$tstr>\n";

    Caution: Contents may have been coded under pressure.
Re: Modifying cookie expire date
by mpeters (Chaplain) on Jan 03, 2005 at 21:43 UTC
    I wouldn't use regex to split/read the timestamp. I would use one of the date handling modules (I like DateTime or Time::Piece) and then perform the date arithmetic with the module. Then spit out the modified timestamp in the format that you want. Something like this with DateTime:
    use DateTime; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new( pattern => '%a, %d %Y %H:%M:%S %z' ); my $dt = $strp->parse_datetime('Sat, 20 Feb 2010 01:31:52 GMT'); $dt->add(days => 1); $dt->set_formatter('%a, %d %Y %H:%M:%S %z'); my $date_string = "$dt";
    Sorry: didn't see the 'cant install other modules' on my scan
      Yes, that it is the problem. I cannot install my own modules. I need to create my own logic to this as simple as date manipulation can be. I'm starting to think it is not easy and why bother, but as always, it's never easy and still people do this by their own :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://419060]
Approved by b10m
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found