php GUID generates functions and classes

  • 2021-01-22 04:56:10
  • OfStack

1. GUID profile
GUID: Globally Unique Identifier (global 1-only identifier), also known as UUID(Universally Unique IDentifier). GUID is a binary 128-bit numeric identifier generated by a specific algorithm to indicate the unionicity of a product. GUID is primarily used to assign identifiers that must be one-unique in a network or system with multiple nodes and multiple 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, and so on.
The format of GUID is "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a 32-bit 106-base number in the range 0-9 or a-f. For example: 6 F9619FF - 8 B86 - D011 - B42D - 00 C04FC964FF GUID value is effective.
2. Advantages of GUID
1.GUID is unique in space and time to ensure different numbers generated in different places in time 1.
2. No two computers in the world will ever generate duplicate GUID values.
3. When GUID is needed, it can be automatically generated by the algorithm and does not need an authority to manage it.
4. ES45en is fixed in length and relatively short, making it ideal for sorting, identification, and storage.
3. GUID generation function


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 generates classes
PHP Obtain the GUID class: 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);    
    }    
}    
// 3 Period of     
// 1 Segment is microseconds  1 Is the address  1 Segments are random numbers     
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 class:


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


Related articles: