PHP产生guid的算法一例

本文介绍了一段使用PHP实现的GUID生成类,详细解释了如何通过当前时间戳、计算机名和IP地址生成全局唯一标识符(GUID),并展示了在实际场景中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码来自:http://koda.iteye.com/blog/669250

因为源代码报错,做了小调整后展示于此:

<?php 
class System {
    static function currentTimeMillis() {
          list($usec, $sec) = explode(" ",microtime());
          return $sec.substr($usec, 2, 3);
    }
}

class NetAddress {
    var $name = 'localhost';
    var $ip    = '127.0.0.1';
    static function getHost($coumputer_name, $ip) { // static
         $address = new NetAddress();
         $address-> name = $coumputer_name;
         $address-> ip   = $ip;

          return $address;
    }

    function toString() {
          return strtolower($this->name.'/'.$this-> ip);
    }
}

class Random {
    static 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($computer_name, $ip){
         $this->getGuid($computer_name, $ip);
    }

    function getGuid($coumputer_name, $ip){
         $address = NetAddress::getHost($coumputer_name, $ip);
         $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);
    }
}

$computer_name = $_SERVER["SERVER_NAME"]; 
$ip            = $_SERVER[ "SERVER_ADDR"]; 
$guid = new Guid($computer_name, $ip); 
print $guid->toString(); 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值