PHP - Number: Generating Unique Identifiers



PHP Number Tips - Part 14: If you want to generate a unique, random numeric identifier that cannot be easily guessed, you can use a combination of PHP's uniqid( ), md5( ), and rand( ) functions:
<?php
// generate a random, unique ID
// result: "5542ec0a1928b99ef90cb87503094fe4" (example)
$id = md5(uniqid(rand(), true));
echo $id;
?>

PHP's uniqid( ) function returns an alphanumeric string based on the current time in microseconds and because the identifier is based in a time value, so there is a very slight possibility of two identical identifiers being generated at the same instant. There are to reduce this possibility and add a random element to the procedure by combining the call to uniqid( ) with a call to rand( ) and md5( ).





Tag: number, unique identifiers Category: PHP Basic Post : March 16th 2008 Read: 663 Bookmark and Share

blog comments powered by Disqus