Re: Creating Unique Values
| From: | Charles Killian | Date: | Wed, 04 Oct 2000 17:24:36 +0000 |
| Subject: | Re: Creating Unique Values | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18623@lists.php.net to get a copy of this message | ||
Here are two functions I use to create unique ids:
The first use unique_id() and md5() as discussed in this thread. (I think I
copied it from the manual).
The second is useful if you want to restrict the unique id to a specific
length and character set ( I think I copied it from Core PHP).
function get_md5_unique_id(){
mt_srand ((double) microtime() * 1000000);
return $unique_id = md5(uniqid(mt_rand(),1));
}// end get_md5_unique_id
function get_unique_id($length=32, $pool=""){
// set pool of possible char
if($pool == ""){
$pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$pool .= "abcdefghijklmnopqrstuvwxyz";
$pool .= "0123456789";
}// end if
mt_srand ((double) microtime() * 1000000);
$unique_id = "";
for ($index = 0; $index < $length; $index++) {
$unique_id .= substr($pool, (mt_rand()%(strlen($pool))), 1);
}// end for
return($unique_id);
}// end get_unique_id
Charles