<?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()
{
$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);
}
}
?>
调用:
<?php
//包含该类文件名
$Guid = new Guid();
print $Guid->toString();
?>
方法二:自己写个函数生成
function uuid() {
$node = env('SERVER_ADDR');
$pid = null; if (strpos($node, ':') !== false) {
if (substr_count($node, '::')) {
$node = str_replace('::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node);
}
$node = explode(':', $node) ;
$ipv6 = '' ; foreach ($node as $id) {
$ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
}
$node = base_convert($ipv6, 2, 10); if (strlen($node) < 38) {
$node = null;
} else {
$node = crc32($node);
}
} elseif (empty($node)) {
$host = env('HOSTNAME'); if (empty($host)) {
$host = env('HOST');
} if (!empty($host)) {
$ip = gethostbyname($host); if ($ip === $host) {
$node = crc32($host);
} else {
$node = ip2long($ip);
}
}
} elseif ($node !== '127.0.0.1') {
$node = ip2long($node);
} else {
$node = null;
} if (empty($node)) {
$node = crc32(Configure::read('Security.salt'));
} if (function_exists('zend_thread_id')) {
$pid = zend_thread_id();
} else {
$pid = getmypid();
} if (!$pid || $pid > 65535) {
$pid = mt_rand(0, 0xfff) | 0x4000;
} list($timeMid, $timeLow) = explode(' ', microtime());
$uuid = sprintf("%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node); return $uuid;
}
这个是CakePHP里面的你自己修改一下就可以用了