Generate Unique Checking Serials

3

Someone knows how I can do a single serial system in a way that never repeats the same serial, I heard that with rand and md5 to do but I'm not sure that one hour it will not happen again.

    
asked by anonymous 05.11.2015 / 17:29

3 answers

3

You can generate a hash "unique" like this:

$serial = hash('sha512', mt_rand());
echo $serial;

Exit:

8011e6c104db93aeba14974ea471d592c7b3662d52e249be89e5bf978bd8bdf039613cfeb3e11dc9b962f2e953bf16ca56bc675d95dd67796413291f04174af4

If you make a strlen($serial) you will see that 128 random characters will be generated, the chance that you will be able to repeat those characters is 2 bilhões computed by the mt_rand() function from 0 to 2147483647.

    
05.11.2015 / 17:40
4

Calling the function with no parameters will generate a unique 13-character ID.

I used this code and got the following results:

<?php

for ($i = 0; $i < 20; $i++)
{
    echo uniqid() , '<br />';
}

Answer:

4e7a2a9eda4f4
4e7a2a9eda513
4e7a2a9eda520
4e7a2a9eda52c
4e7a2a9eda538
4e7a2a9eda53e
4e7a2a9eda545
4e7a2a9eda54a
4e7a2a9eda54f
4e7a2a9eda553
4e7a2a9eda558
4e7a2a9eda55d
4e7a2a9eda562
4e7a2a9eda567
4e7a2a9eda56c
4e7a2a9eda571
4e7a2a9eda576
4e7a2a9eda57b
4e7a2a9eda580
4e7a2a9eda585

Source and more examples: link

    
05.11.2015 / 17:32
3

I usually prefer something simple using timestamp

<?php
// retorna 10 caracters numéricos:
// exemplo: 1446815555
echo time();

The chances of collision

  • It can occur when the environment has a wrong date in the past, where at some point it may coincide with some past timestamp already used. And even in this situation are very small chances.

  • If used in a loop in very fast runs, the chance of collision is very large, since a loop loop may call the time() function more than once under the same millisecond.

  • For this case, of use within a loop of repetition, one can avoid collisions by incrementing a counter

    Example:

    for ($i = 0; $i<5; $++)
        echo time().$i.PHP_EOL;
    

    It will print something like

    14468155550
    14468155551
    14468155552
    14468155553
    14468155554
    

    What style or technique to choose

    The choice of serial type depends on the business model.

    As an example, if the serial is used commercially, for customer support, for example, it is impracticable to present to the client a serial of this type 8011e6c104db93aeba14974ea471d592c7b3662d52e249be89e5bf978bd8bdf039613cfeb3e11dc9b962f2e953bf16ca56bc675d95dd67796413291f04174af4

    Imagine the call asking the customer to spell the serial number on the phone. rsrs

    Finally, there is no better or worse. There is what is most appropriate or least suitable for the business model.

    However, in most cases, the smaller the serial, the better. Among the requirements of being single and having low or no collision possibilities.

    Timestamp lifecycle

    If you use timestamp, be aware of the end of the lifecycle.

    The timestamp has a limit until 2038 and this is not too far away. link

        
    06.11.2015 / 14:36