php GUID生成函数和类


Posted in PHP onMarch 10, 2014

一、GUID简介
GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。
在 Windows 平台上,GUID 广泛应用于微软的产品中,用于标识如如注册表项、类及接口标识、数据库、系统目录等对象。
GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。
二、GUID的优点
1.GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。
2.世界上的任何两台计算机都不会生成重复的 GUID 值。
3.需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。
4.GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。
三、GUID生成函数

function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $hyphen = chr(45);// "-"
    $uuid = chr(123)// "{"
    .substr($charid, 0, 8).$hyphen
    .substr($charid, 8, 4).$hyphen
    .substr($charid,12, 4).$hyphen
    .substr($charid,16, 4).$hyphen
    .substr($charid,20,12)
    .chr(125);// "}"
    return $uuid;
}

三、GUID生成类
PHP获得GUID类:guid_class.php
<?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 getLocalHost() // static    
    {    
        $address = new NetAddress();    
        $address->Name = $_ENV["COMPUTERNAME"];    
        $address->IP = $_SERVER["SERVER_ADDR"];    
        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()    
    {    
        $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类使用方法:

require_once("guid.class.php");    
$Guid = new Guid();    
print $Guid->toString();
PHP 相关文章推荐
PHP strtr() 函数使用说明
Nov 21 PHP
php xml文件操作实现代码(二)
Mar 20 PHP
很让人受教的 提高php代码质量36计
Sep 05 PHP
PHP实现自动登入google play下载app report的方法
Sep 23 PHP
PHP设计模式之适配器模式代码实例
May 11 PHP
PHP实现清除wordpress里恶意代码
Oct 21 PHP
PHP redis实现超迷你全文检索
Mar 04 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
Jul 22 PHP
PHP创建自己的Composer包方法
Apr 09 PHP
laravel获取不到session的三种解决办法【推荐】
Sep 16 PHP
PHP实现统计代码行数小工具
Sep 19 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
Apr 04 PHP
PHP加Nginx实现动态裁剪图片方案
Mar 10 #PHP
php实现文件下载简单示例(代码实现文件下载)
Mar 10 #PHP
php实现文件编码批量转换
Mar 10 #PHP
php导出word文档与excel电子表格的简单示例代码
Mar 08 #PHP
php 创建以UNIX时间戳命名的文件夹(示例代码)
Mar 08 #PHP
PHP_Cooikes不同页面无法传递的解决方法
Mar 07 #PHP
php function用法如何递归及return和echo区别
Mar 07 #PHP
You might like
PHP数组交集的优化代码分析
2011/03/06 PHP
php中记录用户访问过的产品,在cookie记录产品id,id取得产品信息
2011/05/04 PHP
PHP批量生成图片缩略图的方法
2015/06/18 PHP
php上传图片获取路径及给表单字段赋值的方法
2016/01/23 PHP
Yii2框架实现注册和登录教程
2016/09/30 PHP
用js实现的检测浏览器和系统的函数
2009/04/09 Javascript
取选中的radio的值
2010/01/11 Javascript
JavaScript 未结束的字符串常量常见解决方法
2010/01/24 Javascript
使用Microsoft Ajax Minifier减小JavaScript文件大小的方法
2010/04/01 Javascript
jQuery搜索同辈元素方法
2015/02/10 Javascript
JavaScript中创建字典对象(dictionary)实例
2015/03/31 Javascript
JS使用正则表达式除去字符串中重复字符的方法
2015/11/05 Javascript
requireJS使用指南
2016/04/27 Javascript
深入理解js函数的作用域与this指向
2016/05/28 Javascript
微信小程序 使用腾讯地图SDK详解及实现步骤
2017/02/28 Javascript
JS实现根据密码长度显示安全条功能
2017/03/08 Javascript
jQuery插件imgAreaSelect基础讲解
2017/05/26 jQuery
Layui给数据表格动态添加一行并跳转到添加行所在页的方法
2018/08/20 Javascript
jquery判断滚动条距离顶部的距离方法
2018/09/05 jQuery
微信小程序登录按钮遮罩浮层效果的实现方法
2018/12/16 Javascript
Vue组件系列开发之模态框
2019/04/18 Javascript
深入浅出了解Node.js Streams
2019/05/27 Javascript
vue 项目引入echarts 添加点击事件操作
2020/09/09 Javascript
仅用500行Python代码实现一个英文解析器的教程
2015/04/02 Python
Python中的urllib模块使用详解
2015/07/07 Python
Python编程中字符串和列表的基本知识讲解
2015/10/14 Python
python3中os.path模块下常用的用法总结【推荐】
2018/09/16 Python
使用Python快速制作可视化报表的方法
2019/02/03 Python
Python3基础教程之递归函数简单示例
2019/06/07 Python
python库matplotlib绘制坐标图
2019/10/18 Python
python中提高pip install速度
2020/02/14 Python
4s客服专员岗位职责
2013/12/01 职场文书
我的求职计划书
2014/01/10 职场文书
税务会计岗位职责
2014/02/18 职场文书
企业演讲比赛主持词
2014/03/18 职场文书
2019年预备党员的思想汇报:加深对党的认知
2019/09/25 职场文书