Posted By

sendoa on 07/12/06


Tagged

random unique


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

luman
millisami
NexusRex


Unique ID


 / Published in: PHP
 

Clase para generar ID-s únicos de forma automática mediante SHA1 de 40 caracteres

  1. /**
  2. * PHP Class for generating a Unique ID
  3. * 2005-01-10 Replaced with GLOBALS and PID ([email protected])
  4. * 2005-01-02 Added more ingredients to hash ([email protected])
  5. * 2004-12-10 First version ([email protected])
  6. */
  7. class UIDClass
  8. {
  9. // Private variable to hold current ID
  10. private $uid = '';
  11.  
  12. // Return current ID
  13. function getUID(){
  14. // Generate ID if we have not done so already
  15. if($this->uid=='') $this->generateUID();
  16.  
  17. // Return ID
  18. return $this->uid;
  19. }
  20.  
  21. // Generate new ID and store in private variable
  22. function generateUID(){
  23. // Find seconds and microseconds from system clock
  24. list($usec,$sec) = explode(' ',microtime());
  25.  
  26. // Seed the random number generator with above timings
  27. mt_srand((float) $sec + ((float) $usec * 1000000));
  28.  
  29. // Generate hash using GLOBALS and PID
  30. $this->uid = sha1(uniqid(mt_rand(),true).serialize($GLOBALS).getmypid());
  31. }
  32. }

Report this snippet  

You need to login to post a comment.