1. Introduction to GUID
GUID: Globally Unique Identifier (Globally Unique Identifier), also known as UUID (Universally Unique IDentifier). GUID is a 128-bit binary numeric identifier generated by a specific algorithm and used to indicate the uniqueness of a product. GUID is mainly used to assign unique identifiers in a network or system with multiple nodes and computers.
On the Windows platform, GUID is widely used in Microsoft products to identify objects such as registry keys, class and interface identifiers, databases, system directories, etc.
The format of the GUID is "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a 32-digit hexadecimal number in the range of 0-9 or a-f. For example: 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid GUID value.
2. Advantages of GUID
1. GUID is unique in space and time, ensuring that different numbers generated in different places at the same time are different.
2. No two computers in the world will generate duplicate GUID values.
3. When a GUID is needed, it can be completely automatically generated by the algorithm and does not require an authoritative organization to manage it.
4. GUID has a fixed length and is relatively short, which is very suitable for sorting, identification and storage.
3. GUID generation function
Copy code The code is as follows:
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 ;
}
3. GUID generation class
PHP gets the GUID class: guid_class.php
Copy code The code is as follows:
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.bkjia.com/PHPjc/739779.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/739779.htmlTechArticle一、GUID简介 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长...