2

I'm working on a small trivia where you can make your own quiz with your own qustions and answers. I would like to make a URL (game.xx/QfsNS) for each game, and instead of using the id of the game which is just an auto increment id, I would like to have a combination of letters, just like when you URL-shorten URLs.

I tried out SHA-1 but it was too long, and it doesn't have to be that complex.

What should I do?

curly_brackets
  • 5,491
  • 15
  • 58
  • 102
  • possible duplicate of [PHP random string generator](http://stackoverflow.com/questions/4356289/php-random-string-generator) – Barmar Jul 07 '13 at 18:52
  • possible duplicate of [How do sites like goo.gl or jsfiddle generate their URL codes?](http://stackoverflow.com/questions/10299901/how-do-sites-like-goo-gl-or-jsfiddle-generate-their-url-codes) – hjpotter92 Jul 07 '13 at 19:20

5 Answers5

1

It's probably best to use a script like Yourls - http://yourls.org/

That will generate the "short URL style" links you are looking for. You won't have to worry about clashes, or whether your algorithm is sufficiently random.

Pass it the "long" URL of your game - say game.xx/game/football/etc/somthing/98765432 and it will shorten it to game.xx/123abc

Terence Eden
  • 14,034
  • 3
  • 48
  • 89
1

You will need to verify uniqueness in your db table every time you need a new permid.

For example, create a function called get_unique_permid(), and it could look like this:

function get_unique_permid() {
   while (true) {
      $newid = random_string();
      $res = mysql_query("SELECT permid FROM mytable WHERE permid = '$newid'");
      if (mysql_num_rows($res) == 0) break;  // it wasn't in the table, its good
   }

   return $newid;
}

Notice I am using Angy Gee's code for a random_string function, listed in this post. This function will continuously loop, testing random strings, until it finds one that isn't already in your table. It will probably only ever have to try once, maybe twice or even 3 times every now and then, depending on how popular your project is.

I guess if it's super-duper popular, it could even loop through 10 times or more. But at least it guarantees that the permid will be unique.

Richard
  • 1,912
  • 20
  • 28
0

If you're storing the quizzes in a database this may help, it's not great but it works:

function random_string($length=6){
    $str = "";
    $characters = array('B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
    $max = count($characters) - 1;
    for ($i = 0; $i < $length; $i++){
        $rand = rand(0, $max);
        $str .= $characters[$rand];
    }

    return $str;
}
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
0

Shorter and better randomization:

function random_string($length = 6) {
    $str = '';
    $characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
    $max = count($characters) - 1;

    for ($i = 0; $i < $length; $i+=1) {
        $rand = mt_rand(0, $max);
        $str .= $characters[$rand];
    }

    return $str;
}

Alternatively, you could also just generate an MD5-Hash (or any other algo) and shorten it:

echo substr(md5(time()), 0, 6);

The most important part is to check, if the "ID" already exists (collision), or not. The generation process isn't that hard.

You'll have to loop through all database entries, as long, as the generated "ID" exists and as soon, as you found a unique "ID", insert the entry.

user2557188
  • 150
  • 1
  • 8
0

You could write you own hashing algorithm based on the ID of the quiz. If properly written it will guarantee uniqueness.

Roger Far
  • 2,178
  • 3
  • 36
  • 67