欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php中生成标准uuid(guid)的方法

shiping1 的头像
<?php
UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台 会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以 下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相 同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。关于 UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。
在ColdFusion中可以用CreateUUID()函数很简单的生成UUID,其格式为:xxxxxxxx-xxxx-xxxx- xxxxxxxxxxxxxxxx(8-4-4-16),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。而标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)



function guid(){
    if (
function_exists('com_create_guid'
)){
        return
 com_create_guid
();
    }else{
        
mt_srand((double)microtime()*10000);
//optional for php 4.2.0 and up.
        
$charid = strtoupper(md5(uniqid(rand(), true
)));
        
$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
;
    }
}
echo
 guid
();
?>

来自http://blog.sina.com.cn/s/blog_61dfab6b0100ucj5.html


php GUID生成函数和类

作者: 字体:[增加 减小] 类型:转载 时间:2014-03-10 我要评论

这篇文章主要介绍了使用php生成GUID的方法,分别使用了函数和类的方式生成GUID,详细介绍了什么是GUID、GUID的优点等,需要的朋友可以参考下
 

一、GUID简介
GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。
在 Windows 平台上,GUID 广泛应用于微软的产品中,用于标识如如注册表项、类及接口标识、数据库、系统目录等对象。
GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。
二、GUID的优点
1.GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。 
2.世界上的任何两台计算机都不会生成重复的 GUID 值。
3.需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。 
4.GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。
三、GUID生成函数

复制代码代码如下:

function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $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;
}

三、GUID生成类
PHP获得GUID类:guid_class.php

复制代码代码如下:

<?php    
class System    
{    
    function currentTimeMillis()    
    {    
        list($usec, $sec) = explode(" ",microtime());    
        return $sec.substr($usec, 2, 3);    
    }    
}    
class NetAddress    
{    
    var $Name = 'localhost';    
    var $IP = '127.0.0.1';    
    function getLocalHost() // static    
    {    
        $address = new NetAddress();    
        $address->Name = $_ENV["COMPUTERNAME"];    
        $address->IP = $_SERVER["SERVER_ADDR"];    
        return $address;    
    }    
    function toString()    
    {    
        return strtolower($this->Name.'/'.$this->IP);    
    }    
}    
class Random    
{    
    function nextLong()    
    {    
        $tmp = rand(0,1)?'-':'';    
        return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);    
    }    
}    
// 三段    
// 一段是微秒 一段是地址 一段是随机数    
class Guid    
{    
    var $valueBeforeMD5;    
    var $valueAfterMD5;    
    function Guid()    
    {    
        $this->getGuid();    
    }    
    //    
    function getGuid()    
    {    
        $address = NetAddress::getLocalHost();    
        $this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();    
        $this->valueAfterMD5 = md5($this->valueBeforeMD5);    
    }    
    function newGuid()    
    {    
        $Guid = new Guid();    
        return $Guid;    
    }    
    function toString()    
    {    
        $raw = strtoupper($this->valueAfterMD5);    
        return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);    
    }    
}

 

GUID类使用方法:

复制代码代码如下:

require_once("guid.class.php");    
$Guid = new Guid();    
print $Guid->toString();

来自 http://www.jb51.net/article/47892.htm

php生成GUID

liuyan814 发布于 2011年05月21日 8时, 0评/10849阅
 
分享到: 
GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。
 
在 Windows 平台上,GUID 广泛应用于微软的产品中,用于标识如如注册表项、类及接口标识、数据库、系统目录等对象。 
GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。 
★GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。 ★世界上的任何两台计算机都不会生成重复的 GUID 值。★需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。 ★GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。 
 
标签: GUID
 

代码片段(1)[全屏查看所有代码]

1. [代码][PHP]代码     

1
2
3
4
5
6
7
8
9
10
11
12
function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $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;
}

来自 http://www.oschina.net/code/snippet_34463_4414

PHP生成不重复标识符的方法

投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-11-21 我要评论

这篇文章主要介绍了PHP生成不重复标识符的方法,涉及时间函数time的md5转换,uniqid()函数及GUID的应用,非常具有实用价值,需要的朋友可以参考下
 

本文实例讲述了PHP生成不重复标识符的方法。分享给大家供大家参考。具体实现方法如下:

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

PHP倒是自带了生成唯一id的函数:uniqid() ,它是基于当前时间微秒数的,用法如下:

复制代码代码如下:
echo uniqid(); //13位的字符串
echo uniqid("php_"); //当然你可以加上前缀
echo uniqid("php_", TRUE); //如果第二个参数more_entropy为true则生成23位字符串

但是它生成的标识有可能不是唯一的,所以很多人会:
复制代码代码如下:
<?php
    //这是第一种简单的方法,当然用sha1()函数也可以。
    echo md5(uniqid());
    //第二种,利用时间戳的方法
    echo md5(time() . mt_rand(1,1000000));
?>

例子:
复制代码代码如下:
<?
//生成唯一标识符
//sha1()函数, "安全散列算法(SHA1)"
function create_unique() {
    $data = $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']
     .time() . rand();
    return sha1($data);
    //return md5(time().$data);
    //return $data;
}
?>

例子如下:
复制代码代码如下:
<?php
$newhash = create_unique();
echo $newhash; 
?>

我看到很多人使用 md5() 函数,即使它并不完全意味着这个目的:
复制代码代码如下:
// generate unique string
echo md5(time() . mt_rand(1,1000000));
There is actually a PHP function named uniqid() that is meant to be used for this.
// generate unique string
echo uniqid();
/* prints
4bd67c947233e
*/
// generate another unique string
echo uniqid();
/* prints
4bd67c9472340
*/

你可能会注意到,尽管字符串是唯一的,前几个字符却是类似的,这是因为生成的字符串与服务器时间相关。

 

但实际上也存在友好的一方面,由于每个新生成的 ID 会按字母顺序排列,这样排序就变得很简单。

为了减少重复的概率,你可以传递一个前缀,或第二个参数来增加:

复制代码代码如下:
// with prefix
echo uniqid('foo_');
/* prints
foo_4bd67d6cd8b8f
*/
// with more entropy
echo uniqid('',true);
/* prints
4bd67d6cd8b926.12135106
*/
// both
echo uniqid('bar_',true);
/* prints
bar_4bd67da367b650.43684647
*/

这个函数将产生比 md5() 更短的字符串,节省一些空间。

 

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程序设计有所帮助。

来自 http://www.jb51.net/article/57686.htm


php 生成唯一id的几种解决方法

作者: 字体:[增加 减小] 类型:转载 时间:2013-03-08 我要评论

本篇文章介绍了“php 生成唯一id的几种解决方法”,需要的朋友可以参考一下
 

网上查了下,有很多的方法

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

  这种方法有一定的概率会出现重复

2、php内置函数uniqid()

  uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.

  w3school参考手册有一句话:"由于基于系统时间,通过该函数生成的 ID 不是最佳的。如需生成绝对唯一的 ID,请使用 md5() 函数"。

  下面方法返回结果类似: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;
    }
}

com_create_guid()是php自带的生成唯一id方法,php5之后貌似已经没有了。
3、官方uniqid()参考手册有用户提供的方法,结果类似:{E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}

 

 

复制代码代码如下:

public function create_guid($namespace = '') {     
    static $guid = '';
    $uid = uniqid("", true);
    $data = $namespace;
    $data .= $_SERVER['REQUEST_TIME'];
    $data .= $_SERVER['HTTP_USER_AGENT'];
    $data .= $_SERVER['LOCAL_ADDR'];
    $data .= $_SERVER['LOCAL_PORT'];
    $data .= $_SERVER['REMOTE_ADDR'];
    $data .= $_SERVER['REMOTE_PORT'];
    $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
    $guid = '{' .   
            substr($hash,  0,  8) . 
            '-' .
            substr($hash,  8,  4) .
            '-' .
            substr($hash, 12,  4) .
            '-' .
            substr($hash, 16,  4) .
            '-' .
            substr($hash, 20, 12) .
            '}';
    return $guid;
  }

来自 http://www.jb51.net/article/34636.htm
普通分类: