Do not use the uniqid substring. This does not guarantee uniqueness; if I gave you a set of unique numbers {101, 102, 103}, and you took the first two digits and assumed that they would be unique ...
Indeed, you are using PHP. Set up a database table in which you store the generated unique codes, index them and check them when you create new, completely random codes. It will cost O (log (n)) time, where n is the number of codes that you have already generated. If you do not want this, save the previously created codes in a hash table. But without recalling your previous results, you cannot guarantee uniqueness.
source share