Looking at the previous discussions on this, I've seen the code below. I was wondering if you were in a mod_perl enviroment, couldn't the same process call the function to generate the session twice in a second? I know with the random component that it's unlikely to have a collision but to be safe I've been using Timer::HiRes, is this overkill?
# How I've seen it suggested
use MD5;
sub generate_id {
return substr(MD5->hexhash(time(). {}. rand(). $$. 'blah'), 0, 16)
+;
}
# Is this better or overkill?
use Time::HiRes qw(gettimeofday usleep);
use Digest::MD5 qw(md5_hex);
sub generate_sessionID {
my ($s, $usec) = gettimeofday; # Get time of day in seconds and
+ useconds
usleep(1); # Sleep for one usec so a persistant process can't c
+all it twice in a usec (for mod_perl)
# Sessionkey is MD5 hash of seconds_since_epoch+usecs+process_id+r
+andom
return substr(md5_hex($s.$usec.$$.rand()), 0, 32);
}
-Lee
"To be civilized is to deny one's nature."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.