note 27790 added to function.uniqid
| From: | /r/q/u/i/n/n at rack1 dot php dot net | Date: | Wed, 18 Dec 2002 21:26:28 +0000 |
| Subject: | note 27790 added to function.uniqid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41078@lists.php.net to get a copy of this message | ||
My way of generating an ID (32 char string) for use as session identifiers. Just call
NewSessionIdentifier() and use the return value as you like.
//srand just once please
srand((double)microtime()*1000000);
//return a (pseudo) random string of specified length, default=114
function RandomString( $passwordLength=114) {
$password = "";
for ($index = 1; $index <= $passwordLength; $index++) {
// Pick random number between 1 and 62
$randomNumber = rand(1, 62);
// Select random character based on mapping.
if ($randomNumber < 11)
$password .= Chr($randomNumber + 48 - 1); // [ 1,10] => [0,9]
else if ($randomNumber < 37)
$password .= Chr($randomNumber + 65 - 10); // [11,36] => [A,Z]
else
$password .= Chr($randomNumber + 97 - 36); // [37,62] => [a,z]
}
return $password;
}
//Returns randomness as string
function NewSessionIdentifier()
{
//MD5 it (always 32 bytes long)
return (string) md5(uniqid (RandomString(114),1 ));
}
--
http://www.php.net/manual/en/function.uniqid.php
http://master.php.net/manage/user-notes.php?action=edit+27790
http://master.php.net/manage/user-notes.php?action=delete+27790
http://master.php.net/manage/user-notes.php?action=reject+27790