1

How to generate a 10 characters random string in PHP including both capital letters and numbers which will never be duplicated or has the least chance of getting duplicated? For example: 34KJ2SCFPS

If I consider only digits, then can $randon = time(); be ever duplicated? As time changes every millisecond I don't this its possible. Still, any chance?

Ajean
  • 5,528
  • 14
  • 46
  • 69
Aurazo Script
  • 115
  • 1
  • 1
  • 9
  • 1
    Have you tried anything yet? If so, show us the code and let us know what's not working. – Chin Leung May 08 '18 at 20:07
  • @ChinLeung using time() at the moment which gives 10 digit numbers. can it ever get duplicated? I guess not... – Aurazo Script May 08 '18 at 20:09
  • 1
    @AurazoScript but it's not random – Spoody May 08 '18 at 20:10
  • @Mehdi not being random is not an issue as it has nothing to do with security. This will be the referral code of the user. I wanted it both characters and string but even time() will do unless their is a possibility of getting duplicated. which I do not think is. – Aurazo Script May 08 '18 at 20:12
  • 1
    Possible duplicate of [PHP: How to generate a random, unique, alphanumeric string?](https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string) – Spoody May 08 '18 at 20:14
  • Not enough clear – Abe May 08 '18 at 20:15

4 Answers4

31

Use the below code.

For digits and characters

$length = 10;    
echo substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,$length);

For only characters

$length = 10;    
echo substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,$length);

For only digits

$length = 10;    
echo substr(str_shuffle('0123456789'),1,$length);
Praveen
  • 602
  • 4
  • 7
7

If it's not for security you can just md5 time() or microtime()

$rand = md5(microtime())

You'll get a 32 character string that appears random and shouldn't repeat.

Brandon
  • 166
  • 5
3

You can use this code (copied from the String helper in CodeIgniter)

/**
     * Create a "Random" String
     *
     * @param   string  type of random string.  basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
     * @param   int number of characters
     * @return  string
     */
    function random_string($type = 'alnum', $len = 8)
    {
        switch ($type)
        {
            case 'basic':
                return mt_rand();
            case 'alnum':
            case 'numeric':
            case 'nozero':
            case 'alpha':
                switch ($type)
                {
                    case 'alpha':
                        $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                        break;
                    case 'alnum':
                        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                        break;
                    case 'numeric':
                        $pool = '0123456789';
                        break;
                    case 'nozero':
                        $pool = '123456789';
                        break;
                }
                return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
            case 'md5':
                return md5(uniqid(mt_rand()));
            case 'sha1':
                return sha1(uniqid(mt_rand(), TRUE));
        }
    }

This:

echo random_string('alnum', 10);

Will result in something similar to your expected result.

Spoody
  • 2,852
  • 1
  • 26
  • 36
  • are baap re baap... such a lengthy code for such a small task? all that i wanted was a 10 characters random alphanumeric string... and answer by Pravin completes it all.. However, thanks a lot for the time :) – Aurazo Script May 09 '18 at 14:17
  • 1
    @Aurazo my point was you can either use the whole function to generate your string or copy just the related part. – Spoody May 09 '18 at 21:22
0

I use this (in a Model type class) but the principal is the same

 /**
     * UUID - generateds an SQL type unique identifier
     */public static function UUID()
    {
        return sha1(crypt(uniqid(), random_int(1000000, 9999999)));
    }
Datadimension
  • 872
  • 1
  • 12
  • 31