Kid Diamond
Kid Diamond

Reputation: 2301

Create Random Serial with PHP

I want to have a random serial created on my website everytime someone visits.

The format of the serial should be XXXXX-XXXXX-XXXXX-XXXXX.

X represents a random number or capital letter.


Unfortunately I have no idea how to do this. Could anybody please help me out?

So for example the random serial output could be: 3WT4A-NB34O-JU87P-B3UHS


Thanks a lot!

Upvotes: 8

Views: 13736

Answers (7)

salathe
salathe

Reputation: 51950

Another approach is to calculate the four segments as random numbers in base 36.

function generate_serial() {
    static $max = 60466175; // ZZZZZZ in decimal
    return strtoupper(sprintf(
        "%05s-%05s-%05s-%05s",
        base_convert(random_int(0, $max), 10, 36),
        base_convert(random_int(0, $max), 10, 36),
        base_convert(random_int(0, $max), 10, 36),
        base_convert(random_int(0, $max), 10, 36)
    ));
}

Upvotes: 12

maiorano84
maiorano84

Reputation: 11971

There are most certainly better ways of doing it, but this is the first that jumps to mind:

$chars = array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$serial = '';
$max = count($chars)-1;
for($i=0;$i<20;$i++){
    $serial .= (!($i % 5) && $i ? '-' : '').$chars[rand(0, $max)];
}

05/26/13 Edit: It's advised to keep the Count function outside of the loop. The reason for this is that you don't want to expend the extra clock cycles running this function through each iteration when you can simply run it once.

Upvotes: 5

blafrat
blafrat

Reputation: 349

The most straightforward solution would be something like this.

$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$serial = '';

for ($i = 0; $i < 4; $i++) {
    for ($j = 0; $j < 5; $j++) {
        $serial .= $tokens[rand(0, 35)];
    }

    if ($i < 3) {
        $serial .= '-';
    }
}

echo $serial;

Sample output:

51C8A-P9NZD-UM37Q-YKZHO

Upvotes: 6

Nobwyn
Nobwyn

Reputation: 8685

If you want to use this serial just because of uniqueness - you could also try to use UUID v4, as described in one of answers here: PHP function to generate v4 UUID

Upvotes: 2

Moyed Ansari
Moyed Ansari

Reputation: 8461

Try this

function rand_string( $length ) {
    $length += 3;
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
    $str= "";
    $size = strlen( $chars );
    for( $i = 0; $i < $length; $i++ ) {
            if($i == 5 || $i == 11 || $i == 17 )
                $str .= '-';
            else
                $str .= $chars[ rand( 0, $size - 1 ) ];
    }

    return $str;
}

$getstring = rand_string(20,5);
echo $getstring;

Upvotes: 2

John Conde
John Conde

Reputation: 219874

There's a bunch of ways to approach this. One simple one is:

$string1 = substr(md5(uniqid(rand(), true)), -5, 5);
$string2 = substr(md5(uniqid(rand(), true)), -5, 5);
$string3 = substr(md5(uniqid(rand(), true)), -5, 5);
$string4 = substr(md5(uniqid(rand(), true)), -5, 5);
$serial  = sprintf('%s-%s-%s-%s', $string1, $string2, $string3, $string4);

There are better ways to generate random strings then this but this would give you the basic functionality you're looking for.

Upvotes: 4

kuba
kuba

Reputation: 7389

There a lot of such answers at SO: https://stackoverflow.com/a/4356295/1091195

You can use the random string generator mentioned there, either you use it once to get a 20 letter string then insert dashes, or you can call it 4 times for 5 letter and concat the results.

Upvotes: 1

Related Questions