need help:php script to generate universally unique identifier (UUID)

Discussion in 'PHP' started by ikipei, Jun 10, 2008.

  1. #1
    I need to create 32 bytes unique identifier, just like libuuid from the ext2utils project (C library).
    I've found that there's a package that is a wrapper of libuuid, but i dont have permission to install any php module / package.
    so, is there any php script i can use?
     
    ikipei, Jun 10, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    You need something like this?

    
    <?php
    // better, difficult to guess
    $str = strtoupper(md5(uniqid(rand(), true)));
     
    $str = substr($str,0,8) . "-" .
           substr($str,8,4) . "-" .
           substr($str,12,4). "-" .
           substr($str,16,4). "-" .
           substr($str,20);
     
    echo $str;
    ?>
    
    Code (markup):
    from my dutch weblog : http://blog.ericbruggema.nl/2008/05/23/php-guid-maken/
     
    EricBruggema, Jun 10, 2008 IP
  3. ikipei

    ikipei Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow, it's cool.
    thx.
     
    ikipei, Jun 10, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    If you are storing these for user or other identification, you should also verify that a newly generated one isn't currently being used. Although small, there is still a slight chance of having an id collision.
     
    jestep, Jun 10, 2008 IP