How can I generate a unique Key and insert it into my mysql database?
This is what I currently use:
echo uniqid(time(), true);
Can you tell me wether it is a unique key or not?
How can I generate a unique Key and insert it into my mysql database?
This is what I currently use:
echo uniqid(time(), true);
Can you tell me wether it is a unique key or not?
you can use following to get unique id with 32 chars
md5(uniqid(time(), true))
or you can use sha1 like with 40 chars
sha1(uniqid(time(), true))
md5 does not always create a unique hash. It has collisions.Try this for a unique string
function gen_random_string($length=16)
{
$chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";//length:36
$final_rand='';
for($i=0;$i<$length; $i++)
{
$final_rand .= $chars[ ran
d(0,strlen($chars)-1)];
}
return $final_rand;
}
echo gen_random_string()."\n"; //generates a string
echo gen_random_string(8)."\n"; //generates a string with length 8