Short unique id in php

前端 未结 16 967
没有蜡笔的小新
没有蜡笔的小新 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: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.

提交回复
热议问题