SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    GUID generation?

    Does anyone know of a function/library out there for generating GUIDs in PHP? Looking to do some automatic code signing that needs 'em. No results for GUID at PEAR.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I found these that maybe of some help.

    http://www.php.net/manual/nl/function.uniqid.php
    http://jasonfarrell.com/misc/guid.php

    For UUIDs I personally use:
    PHP Code:
    function uuid() {
        return 
    sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            
    mt_rand(00xffff), mt_rand(00xffff),
            
    mt_rand(00xffff),
            
    mt_rand(00x0fff) | 0x4000,
            
    mt_rand(00x3fff) | 0x8000,
            
    mt_rand(00xffff), mt_rand(00xffff), mt_rand(00xffff)
        );

    But the difference between UUID and GUID is unknown to me.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Things one does when bored

    PHP Code:
    <?php

    header
    ('content-type: text/plain');

    $id = new Identity('uuid');
    print 
    $id "\n\n";

    $id->type(Identity::GUID); # ('uuid'|'guid'|1|2|Identity::UUID|Identity::GUID)
    $id->recreate();
    print 
    $id;
    PHP Code:
    <?php

    class Identity {

        const 
    UUID 1;
        const 
    GUID 2;

        const 
    RAW      0;
        const 
    BASE64   1;
        const 
    UUENCODE 2;

        private 
    $_type;
        private 
    $_uuid;

        public function 
    __construct ($type null) {

            if (!
    is_null($type)) {
                
    $this->type($type);
            }

        }

        
    //--------------------------------------------------------------------------

        
    private function _generate () {

            switch (
    $this->_type) {

                case 
    self::UUID:
                    
    $this->_uuid $this->_uuid();
                break;

                case 
    self::GUID:
                    
    $this->_uuid $this->_guid();
                break;

                default:
                    throw new 
    Exception('Cannot call generate with out setting a type.');
                break;

            }

        }

        private function 
    _uuid () {

            return 
    sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                        
    mt_rand(00xffff), mt_rand(00xffff),
                        
    mt_rand(00xffff),
                        
    mt_rand(00x0fff) | 0x4000,
                        
    mt_rand(00x3fff) | 0x8000,
                        
    mt_rand(00xffff), mt_rand(00xffff), mt_rand(00xffff)
                    );

        }

        private function 
    _guid () {

                
    $r '';

                for (
    $i 1$i <= 16$i++) {

                    
    $b = (int)rand(00xff);

                    if (
    $i == 7) {$b &= 0x0f;$b |= 0x40;}
                    if (
    $i == 9) {$b &= 0x3f;$b |= 0x80;}

                    
    $r .= sprintf('%02s'base_convert($b1016));

                    if (
    $i == || $i == || $i == || $i == 10) {
                        
    $r .= '-';
                    }

                }

                return 
    $r;

        }

        public function 
    recreate () {
            
    $this->_generate();
        }

        
    //--------------------------------------------------------------------------

        
    public function type ($type) {

            if (
    is_numeric($type)) {

                
    $this->_type = (int)$type ? (int)$type self::UUID;

            } else if (
    is_string($type)) {

                switch (
    strtolower($type)) {

                    case 
    'guid':
                        
    $this->_type self::GUID;
                        break;

                    case 
    'uuid':
                    default:
                        
    $this->_type self::UUID;
                    break;

                }

            }

            
    $this->_generate();

        }

        public function 
    typeUUID () {
            
    $this->type(self::UUID);
        }

        public function 
    typeGUID () {
            
    $this->type(self::GUID);
        }

        
    //--------------------------------------------------------------------------

        
    private function _retrive ($form) {

            switch (
    $form) {

                case 
    self::BASE64:
                    return 
    base64_encode($this->_uuid);
                break;

                case 
    self::UUENCODE:
                    return 
    convert_uuencode($this->_uuid);
                break;

                case 
    self::RAW:
                default:
                    return 
    $this->_uuid;
                break;

            }

        }

        public function 
    retrive ($form self::RAW) {

            if (
    is_numeric($form)) {
                return 
    $this->_retrive((int)$form);
            }

            if (
    is_string($form)) {

                switch (
    strtolower($form)) {

                    case 
    'base64':
                    case 
    'base':
                    case 
    '64':
                        
    $form self::BASE64;
                    break;

                    case 
    'uuencode':
                    case 
    'uu':
                        
    $form self::UUENCODE;
                    break;

                    case 
    'raw':
                    default:
                        
    $form self::RAW;
                    break;

                }

            } else {
                
    $form self::RAW;
            }

            return 
    $this->_retrive($form);

        }

        public function 
    base64 () {
            return 
    $this->_retrive(self::BASE64);
        }

        public function 
    uuencode () {
            return 
    $this->_retrive(self::UUENCODE);
        }

        
    //--------------------------------------------------------------------------

        
    public function __toString () {
            return 
    $this->_uuid;
        }

    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •