|
一、GUID簡(jiǎn)介
GUID: 即Globally Unique Identifier(全球唯一標(biāo)識(shí)符) 也稱作 UUID(Universally Unique IDentifier) 。 GUID是一個(gè)通過(guò)特定算法產(chǎn)生的二進(jìn)制長(zhǎng)度為128位的數(shù)字標(biāo)識(shí)符,用于指示產(chǎn)品的唯一性。GUID 主要用于在擁有多個(gè)節(jié)點(diǎn)、多臺(tái)計(jì)算機(jī)的網(wǎng)絡(luò)或系統(tǒng)中,分配必須具有唯一性的標(biāo)識(shí)符。
在 Windows 平臺(tái)上,GUID 廣泛應(yīng)用于微軟的產(chǎn)品中,用于標(biāo)識(shí)如如注冊(cè)表項(xiàng)、類及接口標(biāo)識(shí)、數(shù)據(jù)庫(kù)、系統(tǒng)目錄等對(duì)象。
GUID 的格式為“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每個(gè) x 是 0-9 或 a-f 范圍內(nèi)的一個(gè)32位十六進(jìn)制數(shù)。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即為有效的 GUID 值。
二、GUID的優(yōu)點(diǎn)
1.GUID在空間上和時(shí)間上具有唯一性,保證同一時(shí)間不同地方產(chǎn)生的數(shù)字不同。
2.世界上的任何兩臺(tái)計(jì)算機(jī)都不會(huì)生成重復(fù)的 GUID 值。
3.需要GUID的時(shí)候,可以完全由算法自動(dòng)生成,不需要一個(gè)權(quán)威機(jī)構(gòu)來(lái)管理。
4.GUID的長(zhǎng)度固定,并且相對(duì)而言較短小,非常適合于排序、標(biāo)識(shí)和存儲(chǔ)。
三、GUID生成函數(shù)
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
<?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);
}
}
// 三段
// 一段是微秒 一段是地址 一段是隨機(jī)數(shù)
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類使用方法:
復(fù)制代碼 代碼如下:
require_once("guid.class.php");
$Guid = new Guid();
print $Guid->toString();
php技術(shù):php GUID生成函數(shù)和類,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。