note 45662 added to function.microtime

From: Date: Tue, 14 Sep 2004 15:02:50 +0000
Subject: note 45662 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-76670@lists.php.net to get a copy of this message
Do you need a "good" session id to save and retrieve user data? You can get a BIG int containing the timestamp from the Unix Epoch in microseconds: the number of MICROseconds since 1/1/1970 You can use this (of course it's not the only way): <?php list ($usec, $sec) = explode (" ", microtime()); $sid = ($sec) . str_pad ((int)($usec * 1000000), 6, "0", STR_PAD_LEFT); //... ?> It will create a string consisting of only digits like this: "1095173921749619". There will not be two connections sharing this number, but if you want you can add some random at the end: <?php list ($usec, $sec) = explode (" ", microtime()); $sid = ($sec) . str_pad ((int)($usec * 1000000), 6, "0", STR_PAD_LEFT). str_pad (rand (0, 999), 3, "0", STR_PAD_LEFT); //... ?> and for even better safety append the client IP at the end. ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45662 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45662&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45662&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#76670) next »