好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php生成不重复id

生成唯一不重复的标识我们主要是根据当前的一个时间time然后再转换在md5值,这样几乎是可以保证标签的唯一性,下面整理了一些关于PHP生成不重复标识符程序代码

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;
    }
}

php生成全球唯一标识符(GUID)的方法

GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。

世界上的任何两台计算机都不会生成重复的 GUID 值。

需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。

GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。

<?php
//php生成GUID
function getGuid() {
 $charid = strtoupper(md5(uniqid(mt_rand(), true)));
 
 $hyphen = chr(45);// "-"
 $uuid = substr($charid, 0, 8).$hyphen
 .substr($charid, 8, 4).$hyphen
 .substr($charid,12, 4).$hyphen
 .substr($charid,16, 4).$hyphen
 .substr($charid,20,12);
 return $uuid;
}
?>

以上就是php生成不重复id的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于php生成不重复id的详细内容...

  阅读:52次