PHP: How to generate a random, unique, alphanumeric string for use in a secret link?

后端 未结 28 2648
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 22:20

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 y

28条回答
  •  野的像风
    2020-11-21 22:58

    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))
    

提交回复
热议问题