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

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

    Scott, yes you are very write and good solution! Thanks.

    I am also required to generate unique API token for my each user. Following is my approach, i used user information (Userid and Username):

    public function generateUniqueToken($userid, $username){
    
            $rand = mt_rand(100,999);
        $md5 = md5($userid.'!(&^ 532567_465 ///'.$username);
    
        $md53 = substr($md5,0,3);
        $md5_remaining = substr($md5,3);
    
        $md5 = $md53. $rand. $userid. $md5_remaining;
    
        return $md5;
    }
    

    Please have a look and let me know if any improvement i can do. Thanks

提交回复
热议问题