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

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

    I always use this my function to generate a custom random alphanumeric string... Hope this help.

    
    

    It generates for example: Y1FypdjVbFCFK6Gh9FDJpe6dciwJEfV6MQGpJqAfuijaYSZ86g

    If you want compare with another string to be sure that is a unique sequence you can use this trick...

    $string_1 = random_alphanumeric(50);
    $string_2 = random_alphanumeric(50);
    while ($string_1 == $string_2) {
      $string_1 = random_alphanumeric(50);
      $string_2 = random_alphanumeric(50);
      if ($string_1 != $string_2) {
         break;
      }
    }
    echo $string_1;
    echo "
    \n"; echo $string_2;

    it generate two unique strings:

    qsBDs4JOoVRfFxyLAOGECYIsWvpcpMzAO9pypwxsqPKeAmYLOi

    Ti3kE1WfGgTNxQVXtbNNbhhvvapnaUfGMVJecHkUjHbuCb85pF

    Hope this is what you are looking for...

提交回复
热议问题