Home  >  Article  >  Backend Development  >  PHP如何生成唯一数字ID

PHP如何生成唯一数字ID

PHPz
PHPzOriginal
2016-06-06 20:21:275018browse

PHP如何生成唯一数字ID

PHP生成唯一数字ID的方法:

关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?

当然不是,其实有两种方法可以解决。

1. 如果你只用php而不用数据库的话,那时间戳+随机数是最好的方法,且不重复;

2. 如果需要使用数据库,即你还需要给这个id关联一些其他的数据。那就给MySQL数据库中的表的id一个AUTO_INCREMENT(自增)属性,每次插入一条数据时,id自动+1,然后使用mysql_insert_id()或LAST_INSERT_ID()返回这个自增后的id。

当然,这个问题已经有现成的解决方法了,使用php uuid扩展就能完美解决这个问题,这个扩展能生成唯一的完全数字签名。。

如果你不使用composer请参考https://github.com/lootils/uuid,

如果你的项目是基于composer搭建的,那么请参考https://github.com/ramsey/uuid

具体的源码我就不搬运了,小伙伴们自己取下来就可以直接使用了

PHP生成唯一标识符代码示例:

< ? 
//生成唯一标识符  
//sha1()函数, "安全散列算法(SHA1)"  
function create_unique() {  
$data = $_SERVER[&#39;HTTP_USER_AGENT&#39;] . $_SERVER[&#39;REMOTE_ADDR&#39;]  
.time() . rand();  
return sha1($data);  
//return md5(time().$data);  
//return $data;  
} 
?>

PHP生成唯一标识符函数描述及例子

< ?  
$newhash = create_unique();  
echo $newhash;  
?>

再给大家分享一个

/*
 * 信号量(Semaphore)。
 * 这是一个包装类,用于解决不同平台下对“信号量”的不同实现方式。
 * 目前这个类只是象征性的,在 Windows 平台下实际是空跑(并没有真的实现互斥)。
 */
class SemWrapper
{
  private $hasSemSupport;
  private $sem;
  const SEM_KEY = 1;
 
  public function __construct()
  {
    $this->hasSemSupport = function_exists( &#39;sem_get&#39; );
    if ( $this->hasSemSupport ) {
      $this->sem = sem_get( self::SEM_KEY );
    }
  }
 
  public function acquire() {
    if ( $this->hasSemSupport ) {
      return sem_acquire( $this->sem );
    }
    return true;
  }
 
  public function release() {
    if ( $this->hasSemSupport ) {
      return sem_release( $this->sem );
    }
    return true;
  }
}
 
/*
 * 顺序号发生器。
 */
class SeqGenerator
{
  const SHM_KEY = 1;
 
  /**
   * 对顺序号发生器进行初始化。
   * 仅在服务器启动后的第一次调用有效,此后再调用此方法没有实际作用。
   * @param int $start 产生顺序号的起始值。
   * @return boolean 返回 true 表示成功。
   */
  static public function init( $start = 1 )
  {
    // 通过信号量实现互斥,避免对共享内存的访问冲突
    $sw = new SemWrapper;
    if ( ! $sw->acquire() ) {
      return false;
    }
 
    // 打开共享内存
    $shm_id = shmop_open( self::SHM_KEY, &#39;n&#39;, 0644, 4 );
    if ( empty($shm_id) ) {
      // 因使用了 &#39;n&#39; 模式,如果无法打开共享内存,可以认为该共享内存已经创建,无需再次初始化
      $sw->release();
      return true;
    }
 
    // 在共享内存中写入初始值
    $size = shmop_write( $shm_id, pack( &#39;L&#39;, $start ), 0 );
    if ( $size != 4 ) {
      shmop_close( $shm_id );
      $sw->release();
      return false;
    }
 
    // 关闭共享内存,释放信号量
    shmop_close( $shm_id );
    $sw->release();
    return true;
  }
 
  /**
   * 产生下一个顺序号。
   * @return int 产生的顺序号
   */
  static public function next()
  {
    // 通过信号量实现互斥,避免对共享内存的访问冲突
    $sw = new SemWrapper;
    if ( ! $sw->acquire() ) {
      return 0;
    }
 
    // 打开共享内存
    $shm_id = shmop_open( self::SHM_KEY, &#39;w&#39;, 0, 0 );
    if ( empty($shm_id) ) {
      $sw->release();
      return 0;
    }
 
    // 从共享内存中读出顺序号
    $data = shmop_read( $shm_id, 0, 4 );
    if ( empty($data) ) {
      $sw->release();
      return 0;
    }
 
    $arr = unpack( &#39;L&#39;, $data );
    $seq = $arr[1];
 
    // 把下一个顺序号写入共享内存
    $size = shmop_write( $shm_id, pack( &#39;L&#39;, $seq + 1 ), 0 );
    if ( $size != 4 ) {
      $sw->release();
      return 0;
    }
 
    // 关闭共享内存,释放信号量
    shmop_close( $shm_id );
    $sw->release();
    return $seq;
  }
}
 
$a = SeqGenerator::init( time() );
var_dump($a);
 
for ( $i=0; $i < 10; $i++ ) {
  $seq = SeqGenerator::next();
  var_dump($seq);
}

更多相关知识,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn