Generating a unique ID in PHP

前端 未结 3 1884
无人及你
无人及你 2021-02-05 13:33

I\'m trying to generate a unique ID in php in order to store user-uploaded content on a FS without conflicts. I\'m using php, and at the moment this little snippet is responsib

相关标签:
3条回答
  • 2021-02-05 13:53

    Since both uniqid() and rand() are functions based on the current time, rand() adds almost no entropy, since the time will change only a tiny amount between the respective calls.

    As long as you use the more_entropy option, you should never have a collision within a single server. If you use clustering, be sure to include a prefix that differs between servers.

    0 讨论(0)
  • 2021-02-05 13:53

    uniqid() is what you're looking for in most practical situations.

    You can make it even more "uniq" by adding a large random number after it.

    0 讨论(0)
  • 2021-02-05 14:00
    string uniqid ([ string $prefix [, bool $more_entropy ]] )
    

    Gets a prefixed unique identifier based on the current time in microseconds.

    USAGE: $id = uniqid(rand(), true);
    
    0 讨论(0)
提交回复
热议问题