php - does this code guarantee a Unique id -
i'm trying create unique id that's random. solution suggested, i'm not if it's guaranteed they'll unique.
function uid($n) { $id_part = base_convert($n, 10, 36); $rand_part = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'); return sprintf("%05s%.15s", $id_part, $rand_part); }
the code uses unique id database auto-incremented id (that's $n input i'm guessing), fill characters added. gives supposedly "unique" 5 chars base-36 primary id + 15 rubbish chars. can confirm if result indeed unique ids or not.
to answer question, doing exactly. need give users unique id, doesn't directly equal user id in database. way user 1 not know registered before user 2, each user still has have unique "front id".
you can try inbuilt functionalities :
http://php.net/manual/en/function.uniqid.php
http://php.net/manual/en/function.com-create-guid.php
depends how uniqueness want have. if application, or globally.
globally guid better option.
Comments
Post a Comment