首页 > php代码 > asp/php下生成GUID

asp/php下生成GUID

什么是 GUID?

全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装。在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID。


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


为什么要用GUID?


世界上的任何两台计算机都不会生成重复的 GUID 值。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。在 Windows 平台上,GUID 应用非常广泛:注册表、类及接口标识、数据库、甚至自动生成的机器名、目录名等。


asp下生成guid的代码

<%
Dim objTypeLib
Set objTypeLib = CreateObject("Scriptlet.TypeLib")
Response.Write objTypeLib.Guid
%>


php下生成guid要用到class .

guid.class.guid

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

class NetAddress
{
  static $Name = 'localhost';
  static $IP = '127.0.0.1';
  
  static function getLocalHost() // static
  {
    $address = new NetAddress();
    if(isset($_ENV["COMPUTERNAME"]))
    {
        self::$Name = $_ENV["COMPUTERNAME"];
    }
    self::$IP = $_SERVER["SERVER_ADDR"];
    return $address;
  }
  
  static function toString()
  {
    return strtolower(self::$Name.'/'.self::$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()
  {
    $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);
  }
}



guid.php

<?php
    require_once("guid.class.php");
    $Guid = new Guid();
    print $Guid->toString();


本文地址:http://www.phprm.com/code/1192bf30aa032f0b7655e4f1d1c1d791.html

转载随意,但请附上文章地址:-)

标签:guid

发表留言