phpstringrandomuniqueidentifier

How to generate a random, unique, alphanumeric string?


How would it be possible to generate a random, unique string using numbers and letters for use in a verify link? Like when you create an account on a website, and it sends you an email with a link, and you have to click that link in order to verify your account

How can I generate one of those using PHP?


Solution

  • Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

    If you do not need it to be absolutely unique over time:

    md5(uniqid(rand(), true))

    Otherwise (given you have already determined a unique login for your user):

    md5(uniqid($your_user_login, true))