php生成唯一id方法-PHP问题

资源魔 35 0

php天生惟一id办法

1,php内置函数uniqid()

uniqid 用来猎取一个带前缀、基于以后工夫微秒数的惟一ID。

uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] ) : string

注:此函数没有保障前往值的惟一性。 因为绝年夜少数零碎应用 NTP 或许相似效劳调整零碎的工夫,以是零碎工夫常常发作变动。 别的,过程/线程可能没有会前往惟一的 ID。 用 more_entropy 来添加惟一性的几率。

上面办法前往后果相似:5DDB650F-4389-F4A9-A100-501EF1348872

function uuid() {
  if (function_exists ( 'com_create_guid' )) {
    return com_create_guid ();
  } else {
    mt_srand ( ( double ) microtime () * 10000 ); //optional for php 4.2.0 and up.随意数收获,4.2.0当前没有需求了。
    $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //依据以后工夫(微秒计)天生惟一id.
    $hyphen = chr ( 45 ); // "-"
    $uuid = '' . //chr(123)// "{"
substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 );
    //.chr(125);// "}"
    return $uuid;
  }
}

2,md5(time() . mt_rand(1,1000000));

这类办法有肯定的几率会呈现反复

以上就是php天生惟一id办法的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!