Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Unique 8 Character Hex Strings

Tags:

php

mysql

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 incorporate 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.

like image 413
Paul Young Avatar asked Oct 12 '22 04:10

Paul Young


1 Answers

See: PHP short hash like URL-shortening websites 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.

like image 72
Ry- Avatar answered Oct 18 '22 09:10

Ry-