本文介绍了如何使用 PHP 为我的扩展服务器生成唯一 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的服务器上使用 PHP 函数 uniqid().它应该类似于微时间.所以我认为它对于一台服务器来说是独一无二的.正确吗?

I'm using the PHP function uniqid() on my server. It should be something like a microtime. So I think it is unique FOR ONE server. Is it correct?

如果我使用负载均衡器扩展我的服务器,我如何获得唯一 ID?我需要一个少于 31 个字符的字符串.

How can I get a unique id if I scale my server with a loadbalancer? I need a string with less than 31 characters.

谢谢

推荐答案

我建议结合多个熵源.这样您就不会依赖于某些假设(本地 IP 地址不同)或运气(两台服务器不会在同一纳米时间完全相同的事情).

I would suggest combining multiple sources of entropy. This way you wouldn't rely on some assumptions (local IP address being different) or luck (two servers won't do the same thing exactly at the same nanotime).

我想到的事情(并且非常便携,不是特定于平台的):

Things that come to my mind (and are pretty portable, not platform specific):

  • 纳米时间,
  • 打开文件系统中的临时目录并计数那里的文件大小,
  • 当前脚本的文件系统日期时间戳,
  • 运行一个简单的无操作循环并计算其持续时间,
  • ...
  • nanotime,
  • open temp directory in file system and count the file sizes there,
  • current script's file system datetime stamps,
  • run a simple no-op loop and count its duration,
  • ...

毕竟您可以将其用作某些散列函数的输入,只是为了规范化为 30 字节的字符串(例如,输入值的 strval() 串联的 md5sum 的最后 30 个字节).

After all you can use this as an input to some hash function, just to normalize to 30-byte string (e.g. last 30 bytes of md5sum of concatenation of strval() of input values).

这篇关于如何使用 PHP 为我的扩展服务器生成唯一 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:31