/
Unique ID generator for URL

Unique ID generator for URL

When developing mod-rewrite urls then we need to expose item ID from database so that user can view/edit it. Example URL looks like this:

If a need arises to be more cryptic and not expose actual item ID, then random string generator needs to be used.

Solution

Use random string generator by creating kUtil::generateUrlIdentifier() method. Some statistics:

String length6 symbols
Used symbolscase-insensitive English alphabet letters and numbers (26 letters + 10 digits = 36 symbols)
Example2akufw

Possible combinations

36^6 = 2176782336 (2.1 billion)

The common approach used by url shorteners is to:

  1. generate a random number
  2. apply base36 conversion on top of it

We already have random number generator (the kUtil::generateId() method) and the new method will just do this:

return base_convert(self::generateId(), 10, 36);

Luckily the the length of number, that is generated by kUtil::generateId() method is 9 digits, which produces exactly 6 symbol long text at the end.

Related Tasks