我应该如何创建我的 GUID?

发布于 2024-08-05 09:28:21 字数 281 浏览 2 评论 0原文

我要将图像上传到系统,并需要通过非连续的唯一 ID 引用它们。我读过一些有关 GUID 的内容,我想知道在 PHP 中制作 GUID 的最佳方法是什么。我应该 md5() 当前时间戳并对其加盐,还是 PHP 的 uniqueid (http ://www.php.net/manual/en/function.uniqid.php)功能足够了吗?

谢谢!

I'm going to be uploading images to a system and need them to be referenced by a non-sequential unique ID. I've read a little about GUIDs, and I'm wondering what the best approach to making one in PHP is. Should I md5() the current timestamp and salt it, or will PHP's uniqueid (http://www.php.net/manual/en/function.uniqid.php) function be sufficient enough?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第七度阳光i 2024-08-12 09:28:21

编辑:

哎呀!我忘记了我这个古老的答案。为了澄清我的天真造成的混乱(与下面的评论一致):MD5(就像大多数有用的哈希值一样,本质上)是不是单射的,因此不能保证它们的输出对于所有输入都是唯一的。

如果哈希冲突是一个问题(在本例中确实如此),则使用此技术将需要在哈希后检查是否已生成相同的密钥。


由于 uniqid 使用当前时间(以微秒为单位)来生成 guid,因此实际上没有你有可能会两次遇到同样的人。

因此,如果您只是使用它来创建唯一的文件名,uniqid() 就足够了。如果你想防止用户猜测 guid,你不妨加大难度并对其进行 md5。

EDIT:

Yikes! I forgot about this ancient answer of mine. To clarify confusion created by my naivety (consistent with the comments made below): MD5 (like most useful hashes, by their nature) are not injective, so their output is not guaranteed to be unique for all inputs.

If hash collisions are an issue (in this case, they are), using this technique will require checking, after hashing, whether an identical key has already been generated.


Since uniqid uses the current time in microseconds to generate the guid, there is virtually no chance you'll ever run into the same one twice.

So if you're just using it to make unique filenames, uniqid() will be sufficient. If you want to prevent users from guessing the guid, you might as well make it harder and md5 it as well.

输什么也不输骨气 2024-08-12 09:28:21

GUID 是 Microsoft 版本的 UUID。 PHP 的 uniqid 是 UUID 的版本 4。绝对足够好。

GUID is Microsoft's version of UUID. PHP's uniqid is version 4 of UUID. Definitely good enough.

请恋爱 2024-08-12 09:28:21

我还想创建 guid 来调用 .net api,这个函数会生成 guid 格式的密钥,它对我有用

function generateGuid($include_braces = false) {
    if (function_exists('com_create_guid')) {
        if ($include_braces === true) {
            return com_create_guid();
        } else {
            return substr(com_create_guid(), 1, 36);
        }
    } else {
        mt_srand((double) microtime() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));

        $guid = substr($charid,  0, 8) . '-' .
                substr($charid,  8, 4) . '-' .
                substr($charid, 12, 4) . '-' .
                substr($charid, 16, 4) . '-' .
                substr($charid, 20, 12);

        if ($include_braces) {
            $guid = '{' . $guid . '}';
        }

        return $guid;
    }
}

I also want to create guid for calling .net api and this function generate a key in guid format and it works for me

function generateGuid($include_braces = false) {
    if (function_exists('com_create_guid')) {
        if ($include_braces === true) {
            return com_create_guid();
        } else {
            return substr(com_create_guid(), 1, 36);
        }
    } else {
        mt_srand((double) microtime() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));

        $guid = substr($charid,  0, 8) . '-' .
                substr($charid,  8, 4) . '-' .
                substr($charid, 12, 4) . '-' .
                substr($charid, 16, 4) . '-' .
                substr($charid, 20, 12);

        if ($include_braces) {
            $guid = '{' . $guid . '}';
        }

        return $guid;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文