MD5 Function and Unique ID in php
Posted by admin | Posted in C sharp interview questions, Interview Tips & Tricks, J2ee Interview Questions, PHP, Tools, scripts | Posted on 03-04-2009
0
uniqid () function, This function has two parameters we can set. The first is the prefix. This is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified it will return 13 characters, if it is true then 23 characters will be returned.Uniqid (prefix, more_entropy)Sample Code Very Simple To Use
<?phpecho ‘creates a unique id with the about prefix<br>’;
$a = uniqid(about);
echo $a;
echo “<hr>”;
echo ‘creates a longer unique id with the about prefix<br>’;
$b = uniqid (about, true);
echo $b;
echo “<hr>”;
echo ‘creates a unique ID with a random number as a prefix - more secure than a static prefix<br>’;
$c = uniqid (rand (),true);
echo $c;
echo “<hr>”;
echo ‘this md5 encrypts the username from above, so its ready to be stored in your database<br>’;
$md5 = md5($c);
echo $md5;
?>
If you want a unique id then use uniqid. The id’s generated are only unique they are not completely random.If you want completly random id’s then you will need to make your own function, as an md5 or sha1 will not help. As these will provide you with a fixed length string. You could chop an md5 string at a random position, but then you would need to check that this string is unique and not just random
Related posts:












