<?php
class Guid
{
/**
* 系统当前时间
*/
private static function _curTimeMillis() {
list($usec, $sec) = explode(" ",microtime());
return $sec.substr($usec, 2, 3);
}
/**
* 客户端相关信息
*/
private static function _getHost() {
$name = empty($_SERVER["HTTP_USER_AGENT"]) ? 'localhost' : $_SERVER["HTTP_USER_AGENT"];
return strtolower($name . '/' . self::_clientIp());
}
/**
* 客户端IP
*/
private static function _clientIp() {
$ip = (@$_SERVER['HTTP_REALIP'])
? $_SERVER['HTTP_REALIP']
: ((@$_SERVER['HTTP_X_FORWARDED_FOR'])
? $_SERVER['HTTP_X_FORWARDED_FOR']
: @$_SERVER['REMOTE_ADDR']);
if (empty($ip)) $ip = '0.0.0.0';
return $ip;
}
/**
* 随机数
*/
private static function _random() {
$tmp = rand(0,1) ? '-' : '';
return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
}
/**
* 生成GUID字符串
* (长度:32 + 4)
* 三段:一段是微秒, 一段是地址, 一段是随机数
*/
public static function toString() {
if (function_exists('com_create_guid')) {
return com_create_guid();
} else {
$string = md5(self::_getHost() . ':' . self::_curTimeMillis() . ':' . self::_random());
$raw = strtoupper($string);
return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
}
}
}
------------------------------------------------
class Uuid
{
private $_yearCode = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T');
const START_YEAR = 2011;
/**
* 组成结构:年份(1个字符),月份(1个字符[十六进制]),日(2个字符),时间戳(后5字符),微秒(6个字符),随机数(3个字符) = 18个字符
*/
public static function make() {
return $this->_yearCode[intval(date('Y')) - self::START_YEAR].
strtoupper(dechex(date('m'))) . date('d') .
substr(time(), -5) . substr(microtime(), 2, 6) . sprintf('%02d', rand(0, 99));
}
}
请发表评论