PHP生成GUID


        GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符

       GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。

<?

// 三段
// 一段是微秒 一段是地址 一段是随机数
class Guid
{
	//$key  三段  (地址.微秒.随机数)
	private $key;
	private $value;
	private $name = 'localhost';
	private $ip   = '127.0.0.1';
	
	function __construct(){
		$this->name = $_SERVER["SERVER_NAME"];
		$this->ip   = $_SERVER["SERVER_ADDR"];
	}
	
	/**
	 * 根据当前时间戳返回字符串
	 */
	static function currentTimeMillis()
	{
		list($usec, $sec) = explode(" ",microtime());
		return $sec.substr($usec, 2, 3);
	}
	
	/**
	 * 生成随机数
	 */
	static function nextLong()
	{
		$tmp = rand(0,1)?'-':"";
		return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
	}
	
	function toString()
	{
		return strtolower($this->name.'/'.$this->ip);
	}
	
	/**
	 * 生成GUID
	 */
	private function createGuid()
	{
		$this->key = $this->toString().':'.self::currentTimeMillis().':'.self::nextLong();
		$this->value = md5($this->key);
	}
	
    /**
     * 获取GUID,把生成的32位MD5值分割显示
     * @return string
     */
	public function getGuid()
	{
		$this->createGuid();
		$raw = strtoupper($this->value);
		return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
	}
}

$Guid = new Guid();
echo $Guid->getGuid();


537507A5-7439-EF59-C408-A31B1AD8F82C

来自: http://www.pangshu.org/study/php/how-to-get-in-php-guid-string.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值