一个生成GUID的类~~~~~~~~~~~~

类别:编程语言 点击:0 评论:0 推荐:

在www.phpclasses.org下到的,希望对大家有帮助
guid -- 全局唯一标识符
型如  54BB7788-0008-7789-ABAB-112233445566  这个样子的英文与数字的组合

Guid Class
License: GPL

Sample:

$Guid = new Guid();
print $Guid->toString();

Binzy Wu
[email protected]
iZz Soft
2004-07-08  20:26:16  Shanghai
-----------------------------------------------

下面是类的内容

<?

/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */


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

}

?>

本文地址:http://com.8s8s.com/it/it25286.htm