Forum Moderators: coopster & phranque

Message Too Old, No Replies

Session ID

How to generate a good one

         

Dabrowski

12:08 am on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok, how do you guys generate a decent session ID?

I've previously just used the output from

time()
but this site is quite busy and has potential for this method to fail.

phranque

4:59 am on Dec 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could add a sequence number after the time-based number to give some additional uniqueness to play with.
if you are getting less than 100 sessions/second you could use a 2 decimal digit sequence number.
or a 2 hex digit sequence number would give you 256 uniques/second.

perl_diver

5:11 am on Dec 18, 2007 (gmt 0)

10+ Year Member



for random session ID's using perl:

MD5::Disgest

phranque

6:23 am on Dec 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



better to try Digest::MD5 [search.cpan.org]

rocknbil

11:14 am on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A simple method is to get the system process ID, time, and the user's remote address and build something from that.

$id = $$ . $ENV{'REMOTE_ADDR'} . time;
$id =~ s/\.¦\-//g;

(that is a vertical pipe there, a class will work as well, e.g. [\.\-]+)

I prefer to do this rather than include another whole library for such a simple task.

$PROCESS_ID
$PID
$$
[search.cpan.org...]

perl_diver

5:06 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



better to try Digest::MD5

oops, thank you for correcting my post.

Dabrowski

11:31 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks phranque and rocknbil, here's what I've got....

Time + IP + 2 digit random number

Then, cos I don't like to see my IP in cookes/session headers and things.....(and a little 'cos I was bored)....

Convert each digit (0-9) into 4-bit binary, and stuff them into 32-bit ints.

Output the ints as %010u, creating a reverse-engineerable 30 character string that has no obvious bearing on the original.

[edit] I added an XOR to it as it made a more convincing looking number - not so many 0's [/edit]

[edited by: Dabrowski at 11:38 pm (utc) on Dec. 20, 2007]