php - easiest way to generate an unique and random key -
the question not 'how to' generate such key. there tons of answers. simple timestamp others not unique enough chars rand().
what thougth is, why not combine both? if take timestamp in seconds or milliseconds , add random chars (but no numbers) in between, wouldn't result random , unique string?
the question not specific 1 programming language give code made in php:
$token = (string) preg_replace('/(0)\.(\d+) (\d+)/', '$3$1$2', microtime()); $l = strlen($token); $c = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; for($i = 0; $i < $l; $i++){ $token = substr_replace($token, $c[rand(0, 51)], rand(0, $l+$i), 0); }
echo $token;
i'm realy looking forward randomness-pro's answer!
thanks ;) lenny
i prefer generate guid. here code you.
function getguid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand((double)microtime()*10000);//optional php 4.2.0 , up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45);// "-" $uuid = chr(123)// "{" .substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) .chr(125);// "}" return $uuid; } } $guid = getguid(); echo $guid;
here source. http://guid.us/guid/php