Creating a good hash function to reduce URL length in PHP

I am working on URL shortening. The input is a URL, and the output should be a 4-character string (alphanumeric, case sensitive).

I calculated that if I use 4 characters with a case-sensitive alphanumeric key space, I should potentially be able to store the URLs 64 ^ 4 (16777216) until the free space runs out.

I also do not want my shortener URL to generate short URLs that are offensive four letters. It would be unfortunate if someone made a short url that was domain.com/f**k. You will get a picture ...

Any ideas on how best to do this? I feel like I will use base64_encode somewhere in this process.

+3
source share
2 answers

If I were you, I would make a case-sensitive alphanumeric increment. Just increment and assign the number to the database row. To check for bad words, just check the blacklist. If that goes well. If not, just increase it again.

So, instead of a hashing algorithm, they are fine. The first few will look like this:

id   | url
-------------------------
0000 | http://google.com
0001 | http://yahoo.com
0002 | http://example.com
...
000a | http://mail.google.com
000b | http://adobe.com
...
000A | http://microsof.com
...
0010 | http://w3.org
...
00a0 | http://youtube.com
...
00A0 | http://stackoverflow.com

Etc.

Here's a hint of how the function will work: http://us3.php.net/manual/en/function.ord.php

, , , (10 + 26 + 26) ^ 4 = 14776336

. . max , false, false ( ===) .

http://pastebin.com/957KPn4p

+3

, YouTube?. ( ) .

+1

Source: https://habr.com/ru/post/1776572/


All Articles