Short unique id in php

前端 未结 16 865
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 17:34

I want to create a unique id but uniqid() is giving something like \'492607b0ee414\'. What i would like is something similar to what tinyurl gives:

相关标签:
16条回答
  • 2020-11-29 18:18
    function rand_str($len = 12, $type = '111', $add = null) {
        $rand = ($type[0] == '1'  ? 'abcdefghijklmnpqrstuvwxyz' : '') .
                ($type[1] == '1'  ? 'ABCDEFGHIJKLMNPQRSTUVWXYZ' : '') .
                ($type[2] == '1'  ? '123456789'                 : '') .
                (strlen($add) > 0 ? $add                        : '');
    
        if(empty($rand)) $rand = sha1( uniqid(mt_rand(), true) . uniqid( uniqid(mt_rand(), true), true) );
    
        return substr(str_shuffle( str_repeat($rand, 2) ), 0, $len);
    }
    
    0 讨论(0)
  • 2020-11-29 18:19

    You could use the Id and just convert it to base-36 number if you want to convert it back and forth. Can be used for any table with an integer id.

    function toUId($baseId, $multiplier = 1) {
        return base_convert($baseId * $multiplier, 10, 36);
    }
    function fromUId($uid, $multiplier = 1) {
        return (int) base_convert($uid, 36, 10) / $multiplier;
    }
    
    echo toUId(10000, 11111);
    1u5h0w
    echo fromUId('1u5h0w', 11111);
    10000
    

    Smart people can probably figure it out with enough id examples. Dont let this obscurity replace security.

    0 讨论(0)
  • 2020-11-29 18:20

    Here's the routine I use for random base62s of any length...

    Calling gen_uuid() returns strings like WJX0u0jV, E9EMaZ3P etc.

    By default this returns 8 digits, hence a space of 64^8 or roughly 10^14, this is often enough to make collisions quite rare.

    For a larger or smaller string, pass in $len as desired. No limit in length, as I append until satisfied [up to safety limit of 128 chars, which can be removed].

    Note, use a random salt inside the md5 [or sha1 if you prefer], so it cant easily be reverse-engineered.

    I didn't find any reliable base62 conversions on the web, hence this approach of stripping chars from the base64 result.

    Use freely under BSD licence, enjoy,

    gord

    function gen_uuid($len=8)
    {
        $hex = md5("your_random_salt_here_31415" . uniqid("", true));
    
        $pack = pack('H*', $hex);
    
        $uid = base64_encode($pack);        // max 22 chars
    
        $uid = ereg_replace("[^A-Za-z0-9]", "", $uid);    // mixed case
        //$uid = ereg_replace("[^A-Z0-9]", "", strtoupper($uid));    // uppercase only
    
        if ($len<4)
            $len=4;
        if ($len>128)
            $len=128;                       // prevent silliness, can remove
    
        while (strlen($uid)<$len)
            $uid = $uid . gen_uuid(22);     // append until length achieved
    
        return substr($uid, 0, $len);
    }
    
    0 讨论(0)
  • 2020-11-29 18:22

    There are two ways to obtain a reliably unique ID: Make it so long and variable that the chances of a collision are spectacularly small (as with a GUID) or store all generated IDs in a table for lookup (either in memory or in a DB or a file) to verify uniqueness upon generation.

    If you're really asking how you can generate such a short key and guarantee its uniqueness without some kind of duplicate check, the answer is, you can't.

    0 讨论(0)
提交回复
热议问题