CodeIgniter Forums
Unique ID for user directory ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Unique ID for user directory ? (/showthread.php?tid=36541)



Unique ID for user directory ? - El Forum - 12-05-2010

[eluser]mci[/eluser]
Hi

I need a unique ID for user directories. Will
Code:
md5(uniqid());
be the right one for this or do you suggest another solution? I'm thinking about to us this unique ID also for the user activation link.


Unique ID for user directory ? - El Forum - 12-05-2010

[eluser]WanWizard[/eluser]
Try to avoid uniqueid(), it's not very portable. It also generates id's which are hex values of the current time, not necessarily that unique.

Instead, I would go for UUID's. There's a PECL extension for it, and there are also native PHP versions floating around.


Unique ID for user directory ? - El Forum - 12-05-2010

[eluser]mci[/eluser]
Thank you. Do you have a recommendation which extension or class I should use?


Unique ID for user directory ? - El Forum - 12-05-2010

[eluser]WanWizard[/eluser]
PECL is handy, but requires control over the PHP installation (which you might not have in all cases) so you can install it.

Alternatively, you can use this PHP function:
Code:
function gen_uuid() {
    return sprintf( 'xx-x-x-x-xxx',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}
( based on http://www.php.net/manual/en/function.uniqid.php#94959 )