• 生成GUID的算法


    类库
    <?php
    // guid.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 getHost($coumputer_name, $ip) { // static
        $address = new NetAddress();
        $address->name = $coumputer_name;
        $address->ip   = $ip;
    
        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($computer_name, $ip){
        $this->getGuid($computer_name, $ip);
      }
    
      function getGuid($coumputer_name, $ip){
        $address = NetAddress::getHost($coumputer_name, $ip);
        $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
    require_once('guid.php');
    $computer_name = $_SERVER["SERVER_NAME"];
    $ip            = $_SERVER["SERVER_ADDR"];
    $guid = new Guid($computer_name, $ip);
    print $guid->toString();
  • 相关阅读:
    记忆点
    数组的操作
    console.log()中的运算与打印事件
    ie9上传后下载json
    mysql使用on duplicate key update批量更新数据
    vue 弹出菜单
    mysql备份脚本
    uniapp+nodejs微信支付小程序版
    mycat初体验
    vscode格式化html标签属性不换行(vetur插件)
  • 原文地址:https://www.cnblogs.com/chu888chu888/p/2616472.html
Copyright © 2020-2023  润新知