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

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

    This function will generate a random key using numbers and letters:

    function random_string($length) {
        $key = '';
        $keys = array_merge(range(0, 9), range('a', 'z'));
    
        for ($i = 0; $i < $length; $i++) {
            $key .= $keys[array_rand($keys)];
        }
    
        return $key;
    }
    
    echo random_string(50);
    

    Example output:

    zsd16xzv3jsytnp87tk7ygv73k8zmr0ekh6ly7mxaeyeh46oe8
    

提交回复
热议问题