Zend - The PHP Company




Algorithms

Add Code


creating unique ids (strings)  

Type: code fragment
Added by: charles_killian
Entered: 04/10/2000
Last modified: 01/12/2000
Rating: ***** (4 votes)
Views: 9111
Below are two functions I use to create unique ids: The first uses unique_id() and md5(). (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).


<?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

?>


Usage Example




Rate This Script





Search



This Category All Categories