这个php唯一ID生成器有问题吗?

时间:2012-05-11 20:57:04

标签: php code-generation sha1

我打算使用此代码生成故障单的唯一身份用户代码。

// A prefix to avoid uniqid collisions (when invoking this function at the same time).
// Should I use mt_rand or just rand?
$prefix = str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);

// uniqid with more entropy enabled.
$unique_id = uniqid("$prefix-", TRUE);

// A SHA-1 hash for the generated code, this way it looks less sequential.
$unique_id = sha1($unique_id);

如果您能在此代码中告诉我问题,那将会更有帮助。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

这取决于要求,它是否必须是一个没人能猜到的超级秘密字符串?

如果没有,假设您要将票证存储在数据库中,您可以使用$unique_id = sha1(DATABASE_KEY);

echo sha1(1) . "\n";
echo sha1(2) . "\n";
echo sha1(3) . "\n";

See the example

现在你有一个很长的,有趣的字符串,你甚至不需要存储。

相关问题