Just as a fun project I wanted to try and make a simple URL shortener for my own personal use but I wanted to try and incorperate things that I liked from other shorteners like bit.ly and such. So I've come to a snag when it comes to assigning short URL IDs.

Right now I just manually assign the code but I would like to automate it. I could do it the easy way by just assigning incrementing IDs (I thought this could be done using an assigned auto increment value on the MySQL database and just use the PHP dechex() function for the URL) but it seems that other shorteners are random. I know that I won't get an absurd number of URLs in the database but I still want to keep the process efficient which makes creating random unique IDs rather taxing with many URLs in the database. I don't really have any idea about how to go about making a system to make the IDs that doesn't make duplicates and doesn't run slowly. Any pointers or help would be appreciated.

Accepted Answer

See: php short hash and the answer you might want: http://blog.kevburnsjr.com/php-unique-hash

The second link might be particularly useful, just short-hash the current ID.

Written by minitech
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki