0

Currently me and my friend are developing a website, for what we will call 'projects' we just have a basic auto increment id in the database used to navigate to projects such as

oururl.com/viewproject?id=1

but we started thinking, if we have alot of posted projects thats going to be a LONG url. So we need to somehow randomly generate a alphanumerical string about 6 characters long. We want the chance of the string being duplicated being extremely low and of course we will query the database before assigning an identifier. Thanks for anyhelp, means alot!

NardCake
  • 130
  • 2
  • 9
  • 2
    This is exactly what you are looking for: http://kevin.vanzonneveld.net/techblog/article/create_short_ids_with_php_like_youtube_or_tinyurl/ – Tchoupi Sep 22 '12 at 15:37

4 Answers4

2

Keep numeric IDs in your database for speed. Use an algorithm that turns those numeric IDs into alphanumeric IDs.

That way, you don't have to worry about duplicates, and you still get really fast indices in your DB.

See this answer: https://stackoverflow.com/a/12479939/362536 And this question: Tinyurl-style unique code: potential algorithm to prevent collisions

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
  • Good. Also, look at the link Mathieu posted. It's exactly the same concept. The point is to leave numeric indices in your database. They are there for a reason. It's easy to search, there is guaranteed to not be a collision. What you use for display in a URL can be different, and "translating" from alphanumeric to a numeric ID is quick and simple. Don't mess up your DB just for display. Keep those layers separate. – Brad Sep 22 '12 at 15:43
0

You could always use PHP's uniqid() to generate a random string, then trim it down with substr().

$unique_six_character_id = substr(uniqid(), 0, 6);
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Try this:

    $id = substr(md5(uniqid()), 0, 6);
Manolis Agkopian
  • 1,033
  • 13
  • 22
  • 2
    Why would you hash the unique ID? You're increasing the chance of collisions. This is a bad idea. – Brad Sep 22 '12 at 15:39
  • He said that he want it to be an alphanumerical string, just to increase the chance to contain letters. – Manolis Agkopian Sep 22 '12 at 15:44
  • Even without md5() you have to check if the key allready exists, just in case because it's a primary key. So I don't thik that there is a problem. – Manolis Agkopian Sep 22 '12 at 15:50
  • Furthermore the output of the uniqid() if you you use substr(uniqid(), 0, 6) there is big chance to get the exact same result because the output of the uniqid() is based on the current time in microseconds. – Manolis Agkopian Sep 22 '12 at 15:59
  • 1
    You have a bigger chance of getting a collision with `md5()` around it than you do without! – Brad Sep 22 '12 at 16:00
  • @Brad has a point. `substr(md5(whatever), 0, 6)` => collisions waiting to happen. Just use uniqid directly instead. – Mahn Sep 22 '12 at 16:00
  • @Mahn, No, don't use `uniqid()` at all! Please, read my answer. It's the only proper answer to this question posted thus far. – Brad Sep 22 '12 at 16:01
  • I mean if he must. Of course incremental ids > uniqid, just saying that uniqid > md5 trimmed – Mahn Sep 22 '12 at 16:03
  • What about of using just an auto_increment id inside the database and in the urls use the hex value of it? By this way you make urls shorter and you completely avoid collisions. – Manolis Agkopian Sep 22 '12 at 16:19
0

You can use this code to generate dynamic 6 digit number....

$random = substr(number_format(time() * rand(),0,'',''),0,6);

Here’s whats happening with the code: The outer portion “substr ” is used to chop down the random number we create to 6 characters. You will notice the number 10 at the end of the snippet, which can be changed to any number you want.

The “number_format ” function helps get rid of the scientific notation that will arise from generating the random number. In the middle, “time() ” and “rand() ” are multiplied. Time() is the number of seconds from January 1 1970, and rand() is a uniquely generated number through PHP.

Always remember that generating unique numbers is not fool proof. If your application requires each number to be unique, perform a collision check in the database before saving.

Raj Adroit
  • 3,828
  • 5
  • 31
  • 44
  • hm tough decision here. The other one requires more time, this one is quick, but not duplication proof... :D – NardCake Sep 22 '12 at 15:51
  • Except `time() * rand()` is more likely to generate collisions than uniqid alone – Mahn Sep 22 '12 at 16:08