Skip to main content

Generate unique identifier using SHA1 algorithm in PHP.

<?php

/**
 * PHP Class for generating a Unique ID
 * 
 * Generate unique identifier using SHA1 algorithm.
 * 
 * 2005-01-10  Replaced with GLOBALS and PID  (ultrasine@gmail.com)
 * 2005-01-02  Added more ingredients to hash  (ultrasine@gmail.com)
 * 2004-12-10  First version              (ultrasine@gmail.com)
 */
class UIDClass {
  
  /**
   * Private variable to hold current ID.
   * 
   * @var unknown_type
   */
  private $uid = '';
  
  
  /**
   * Return current ID
   * 
   * @return string
   */
  function getUID() {
    // Generate ID if we have not done so already
    if ($this->uid == '') {
      $this->generateUID();
    }
    return $this->uid;
  }
  
  /**
   * Generate new ID and store in private variable.
   */
  function generateUID() {
    // Find seconds and microseconds from system clock.
    list ($usec, $sec) = explode(' ', microtime());
    
    // Seed the random number generator with above timings
    mt_srand((float) $sec + ((float) $usec * 1000000));
    
    // Generate hash using GLOBALS and PID
    $this->uid = sha1(uniqid(mt_rand(), true) . serialize($GLOBALS) . getmypid());
  }
  
}

?>