Yii Framework Forum: MY "UUID" solution : Added Suuid - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

MY "UUID" solution : Added Suuid Rate Topic: -----

#1 User is offline   Q-efx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 12-April 09

Posted 23 May 2009 - 05:22 PM

Hello I wrote a quick UUID solution. In the way i think to archive REAL UUIDs.

Its not so big or special. Just the method is unique ( I think )

It stores the URI of the page, so you know where this UUID comes, to store the URL all - are replaced by _ to keep a "compatible" URL. But i think in 99% of the scenarios where it could be used. This will not be used ^^

Here is the code:


<?php
/*
* A  _GUID_ which should create REAL GUIDs
* Usefull for bigger projects I hope :)
* It stores an appId, the user ip, the uri
* called by the script and the time in unix format
* then appends a _UUID_
*
* For better _URL_ saving, it will replace all - with _
* Keep this in mind, if you use it!
*
* LINK: www.-u-r-reality.de
*/

class EUuidComponent extends CApplicationComponent
{
    private $appId;
    private $uri;
    private $ip;
    private $time;
    private $prefix;

    public function __construct($appId = '0001', $ip = '127.0.0.1', $uri = 'index', $time = 0)
    {
        $this->appId = $appId.'-';
        $this->ip = $_SERVER['REMOTE_ADDR'].'-';
        $this->uri = str_replace('-', '_',$_SERVER['REQUEST_URI'] ).'-';
        $this->time = $_SERVER['REQUEST_TIME'] ? $_SERVER['REQUEST_TIME'] : time().'-';

        $this->prefix = $this->appId.$this->ip.$this->uri.$this->time;
    }

    public function init()
    {
        parent::init();
    }

    protected function createUuid()
    {

        $uuid = 0;

        $chars = md5(uniqid(mt_rand(), true));

        $uuid  = substr($chars,0,8) . '-';

        $uuid .= substr($chars,8,4) . '-';

        $uuid .= substr($chars,12,4) . '-';

        $uuid .= substr($chars,16,4) . '-';

        $uuid .= substr($chars,20,12);

        return $uuid;
    }

    public function getUuid()
    {
        return $this->prefix.'-'.$this->createUuid();
    }

    public function createSuuid($size = 9, $step = 3)
    {

        $sUuid = NULL;

        for($x=0; $x < $size; $x++ )
        {
            if ($x%3 == 0)
            {
                $sUuid .= $x==0 ? NULL : '-';
                $sUuid .= mt_rand(0,9);
            }
            else
                $sUuid .= mt_rand(0,9);
        }

        return $sUuid;

    }


Do what ever you want, as long as you dont claim it as your own. And keep the link to this page. So if there updates, user could check for it. and know where it is from ;)

UPDATE: I added suuid, small uuids, you could use it to create IDs for ICQ like things, for example.

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users