• PHP GUID和UUID生成类


     <?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));
    }
    }
  • 相关阅读:
    二分法查找递归方式()
    JDBC操作MySQL(crud)
    (转)JAVA中的权限修饰符
    抽象类和接口(面试题总结)
    java基础-集合
    Java泛型通配符以及限定
    div中嵌套的多个div使用了浮动后居中的办法
    将博客搬至CSDN
    (补)Java解析XML之dom4j
    Java单元测试
  • 原文地址:https://www.cnblogs.com/rmbteam/p/2205914.html
Copyright © 2020-2023  润新知