PHP下生成GUID(已测)(运维网)
guid.class.php 文件PHP代码<?php classSystem { functioncurrentTimeMillis() { list($usec,$sec)=explode("",microtime()); return$sec.substr($usec,2,3); } } classNetAddress { var$Name='localhost'; var$IP='127.0.0.1'; functiongetLocalHost()//static { $address=newNetAddress(); $address->Name=$_ENV["COMPUTERNAME"]; $address->IP=$_SERVER["SERVER_ADDR"]; return$address; } functiontoString() { returnstrtolower($this->Name.'/'.$this->IP); } } classRandom { functionnextLong() { $tmp=rand(0,1)?'-':''; return$tmp.rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(100,999).rand(100,999); } } //三段 //一段是微秒一段是地址一段是随机数 classGuid { var$valueBeforeMD5; var$valueAfterMD5; functionGuid() { $this->getGuid(); } // functiongetGuid() { $address=NetAddress::getLocalHost(); $this->valueBeforeMD5=$address->toString().':'.System::currentTimeMillis().':'.Random::nextLong(); $this->valueAfterMD5=md5($this->valueBeforeMD5); } functionnewGuid() { $Guid=newGuid(); return$Guid; } functiontoString() { $raw=strtoupper($this->valueAfterMD5); returnsubstr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20); } }
[*]?>
使用
PHP代码<?php require_once("guid.class.php"); $Guid = new Guid(); print $Guid->toString();
[*]?>
页:
[1]