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

后端 未结 28 2282
隐瞒了意图╮
隐瞒了意图╮ 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 23:06

    1. Generate a random number using your favourite random-number generator
    2. Multiply and divide it to get a number matching the number of characters in your code alphabet
    3. Get the item at that index in your code alphabet.
    4. Repeat from 1) until you have the length you want

    e.g (in pseudo code)

    int myInt = random(0, numcharacters)
    char[] codealphabet = 'ABCDEF12345'
    char random = codealphabet[i]
    repeat until long enough
    

提交回复
热议问题