php 如何生成新的 GUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21671179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate a new GUID?
提问by mauzilla
I'm working on a web service which requires a new GUID()passed as a reference to a method within the service.
我正在开发一个 web 服务,它需要一个新的GUID()作为对服务中方法的引用传递。
I am not familiar with C#or the GUID() object, but require something similar for PHP(so create a new object which from my understanding returns an empty/blank GUID).
我不熟悉C#或GUID() object,但需要类似的东西PHP(因此创建一个新对象,根据我的理解返回一个empty/blank GUID)。
Any ideas?
有任何想法吗?
回答by Michel Ayres
You can try the following:
您可以尝试以下操作:
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
回答by Alexey
As an alternative to the above options:
作为上述选项的替代方案:
$guid = bin2hex(openssl_random_pseudo_bytes(16));
It gives a string like 412ab7489d8b332b17a2ae127058f4eb
它给出了一个字符串 412ab7489d8b332b17a2ae127058f4eb
回答by user8240385
<?php
function guid(){
if (function_exists('com_create_guid') === true)
return trim(com_create_guid(), '{}');
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
?>
GUID Generator
GUID 生成器
回答by SimonSimCity
According to Is there any difference between a GUID and a UUID?
GUID is Microsoft's implementation of the UUID standard.
GUID 是 Microsoft 对 UUID 标准的实现。
So, here's a link to libraries, that let's you create UUIDs of the following types:
所以,这里有一个库的链接,让你创建以下类型的 UUID:
- version 1 (time-based)
- version 3 (name-based and hashed with MD5)
- version 4 (random)
- version 5 (name-based and hashed with SHA1)
- 版本 1(基于时间)
- 版本 3(基于名称并使用 MD5 散列)
- 版本 4(随机)
- 版本 5(基于名称并使用 SHA1 散列)
https://github.com/search?p=1&q=uuid+php&ref=cmdform&type=Repositories
https://github.com/search?p=1&q=uuid+php&ref=cmdform&type=Repositories
I don't know exactly, which one C# is using, but that's at least something you can use if you're writing some piece of software and want to have universal unique identifiers.
我不确切知道 C# 正在使用哪一个,但如果您正在编写某些软件并希望拥有通用唯一标识符,那么至少可以使用它。
My perfered choice was https://github.com/fredriklindberg/class.uuid.phpbecause it is just a simple PHP file and the most rated one (https://github.com/ramsey/uuid) had to much dependencies on other libraries, but his may change soon (see https://github.com/ramsey/uuid/issues/20).
我更喜欢的选择是https://github.com/fredriklindberg/class.uuid.php,因为它只是一个简单的 PHP 文件,而且评分最高的文件 ( https://github.com/ramsey/uuid) 对其他图书馆,但他可能很快就会改变(见https://github.com/ramsey/uuid/issues/20)。
But if you really need a GUID (according to the Microsoft standard), they have a different generation process than these 4122. Wikipedia claims that
但是如果你真的需要一个 GUID(根据微软标准),它们的生成过程与这些 4122 不同。维基百科声称
GUIDs and RFC 4122 UUIDs should be identical when displayed textually
当以文本方式显示时,GUID 和 RFC 4122 UUID 应该相同
http://en.wikipedia.org/wiki/Globally_Unique_Identifier#Binary_encoding
http://en.wikipedia.org/wiki/Globally_Unique_Identifier#Binary_encoding
In most cases, you should be fine by going for one of the PHP libs for UUIDs. I don't think you're meddling with Microsoft Component Object Model (COM), don't you?
在大多数情况下,使用 UUID 的 PHP 库之一应该没问题。我不认为您在干预Microsoft 组件对象模型 (COM),不是吗?
回答by Muhammad Naderi
For googlers such as my self, I found this snipet more accurate:
对于像我这样的 googlers,我发现这个 snipet 更准确:
function getGUID(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(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;
}
}
source http://guid.us/GUID/PHP
回答by Simon Rigét
If you just need a very unique ID:
如果您只需要一个非常独特的 ID:
$uid = dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) );
If ID's are generated more than 1 millisecond apart, they are 100% unique.
如果 ID 的生成间隔超过 1 毫秒,则它们是 100% 唯一的。
If two ID's are generated at shorter intervals, this would generate ID's that are 99.999999999999999999% likely to be globally unique (collision in 1 of 10^18)
如果以较短的间隔生成两个 ID,这将生成 99.999999999999999999% 可能是全局唯一的 ID(10^18 中的 1 个冲突)
You can increase this number by adding more digits, but to generate 100% unique ID's you will need to use a global counter.
您可以通过添加更多数字来增加此数字,但要生成 100% 唯一 ID,您需要使用全局计数器。
if you really do need RFC compliance, this will pass as a valid version 4 GUID:
如果您确实需要符合 RFC,这将作为有效的第 4 版 GUID 传递:
$guid = vsprintf('%s%s-%s-4000-8%.3s-%s%s%s0',str_split(dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) ),4));
This follows the intention, but not the letter of the RFC. Among other discrepancies it's a few random digits short. (Add more random digits if you need it) The upside is that this is fast, compared to 100% compliant code. You can test your GUID here
这遵循了意图,但不是 RFC 的字母。在其他差异中,有几个随机数字短。(如果需要,可以添加更多随机数字)与 100% 兼容代码相比,这样做的好处是速度很快。您可以在此处测试您的 GUID

