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

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

    This is a simple function that allows you to generate random strings containing Letters and Numbers (alphanumeric). You can also limit the string length. These random strings can be used for various purposes, including: Referral Code, Promotional Code, Coupon Code. Function relies on following PHP functions: base_convert, sha1, uniqid, mt_rand

    function random_code($length)
    {
      return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $length);
    }
    
    echo random_code(6);
    
    /*sample output
    * a7d9e8
    * 3klo93
    */
    

提交回复
热议问题