/
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:
- http://www.domain.tld/account/topics/edit.html?id=5 - editing page
- http://www.domain.tld/topics/5-topic-title.html - viewing page
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 length | 6 symbols |
---|---|
Used symbols | case-insensitive English alphabet letters and numbers (26 letters + 10 digits = 36 symbols) |
Example | 2akufw |
Possible combinations | 36^6 = 2176782336 (2.1 billion) |
The common approach used by url shorteners is to:
- generate a random number
- 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.