V
V
Vladimir Kudinov2012-10-11 15:47:03
Encryption
Vladimir Kudinov, 2012-10-11 15:47:03

Question about the beauty of links?

Probably everyone knows about link shortening services. They end up with links like <domain>/ DdhDRh . I'm interested in the second part of the link. As far as I understand, this is the id of the record in the table, which was encoded in some way. The first thought was base64, but it also has an equal sign, a plus sign and a slash in the alphabet, which I would not like to see in the link.
Actually the question is: maybe someone knows how they get such beautiful links?
PS: I apologize for the clumsy title. At the end of the working day, it turned out to be difficult to come up with something more meaningful =)

Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
Denis, 2012-10-11
@frux

I just do hex(record.id)[2:] (python) on my bike shortener. It turns out something like 98L as a shortened link.

W
WildZero, 2012-10-11
@WildZero

Somehow they asked me to make a shortcut. The second part of the link was just a 62 representation of the ID.

S
Sergey, 2012-10-11
Protko @Fesor

The usual good old ID (number with autoincrement) + transfer to another number system. 62 is more than enough, as suggested above.

S
Sicness, 2012-10-11
@Sicness

I'm not in the subject, but it's obvious that this is not coding, but just an ID. Since this is a one-way algorithm, where the final options are much smaller than the initial ones, there can obviously be collisions, that is, the choice of the algorithm is not important, you can simply perceive it as an ID. And it’s also logical to assume, from the above, that it’s better to just make an id increment so that you don’t suffer with matches.

M
mejedi, 2012-10-11
@mejedi

pwgen + check for uniqueness?

N
notRly, 2012-10-17
@notRly

Yes, the above is correct. Here is an example implementation in php:

  public static function uniq($number)
  {
      $out   = '';
      $codes = '23456789abcdefghjkmnpqrstuvwxyz';

      while ($number > 30) {
          $key    = $number % 31;
          $number = floor($number / 31) - 1;
          $out    = $codes{$key}.$out;
      }

      return $codes{$number}.$out;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question