How to create an unidentified "tiny URL" based on identifier?

I am interested in creating tiny urls. My idea was to simply save the incremental identifier for each long URL, and then convert that id to its base variant 36, like the following in PHP, for example:

$tinyurl = base_convert($id, 10, 36) 

The problem is that the result is valid, while it's hard to guess what the next URL will be, although it will be short (tiny). For example. atm, if my last tinyurl was a1, the next will be a2. This is bad for me.

So, how would I make sure that the resulting tiny URL is not so guessed, but still short?

+6
php tinyurl url-shortener base36
source share
9 answers

What you're asking for is a balance between reducing information (the URLs of their indexes in your database) and artificially increasing information (to create holes in your sequence).

You must decide how important both are to you. Another question is whether you want consecutive URLs to be guessable or random enough to make it difficult to guess any valid URL.

Basically, you want to declare n of N valid identifiers. Choose N less to shorten the URLs and lower n to create URLs that are hard to guess. Make n and N bigger to generate more urls when shorter ones are executed.

To assign identifiers, you can simply take an arbitrary generator or hash function and limit it to the target range N. If you find a collision, select the following random value. If you have reached the number n of unique identifiers, you should increase the range of your set of identifiers (n and N).

+9
source share

I would just crc32 url

 $url = 'http://www.google.com'; $tinyurl = hash('crc32', $url ); // db85f073 

cons: 8 character constant identifier

+5
source share

It is really cheap, but if the user does not know that this is happening, then it is not so guessed, but the prefix and postfix actual identifier with 2 or 3 random numbers / letters.

If I saw 9d2a1me3, I would not have guessed that dm2a2dq2 was next in the series.

+4
source share

Try Xor'ing $ id with some value, for example. $id ^ 46418 - and to return back to the original identifier, you simply execute the same Xor again, i.e. $mungedId ^ 46418 . Add this along with your base_convert and possibly with some character replacement in the resulting string, and it will be quite difficult to guess the URL.

+2
source share

Another way is to set the maximum number of characters for the URL (say, n ). Then you can choose a random number between 1 and n !, which will be your permutation number.

At which new URL do you increment the identifier and use the permutation number to bind the actual identifier to be used. Finally, you would base 32 (or something else) on the encoding of your URL. That would be completely random and completely reversible.

+2
source share

If you need an injective function, you can use any form of encryption. For example:

 <?php $key = "my secret"; $enc = mcrypt_ecb (MCRYPT_3DES, $key, "42", MCRYPT_ENCRYPT); $f = unpack("H*", $enc); $value = reset($f); var_dump($value); //string(16) "1399e6a37a6e9870" 

Cancel:

 $rf = pack("H*", $value); $dec = rtrim(mcrypt_ecb (MCRYPT_3DES, $key, $rf, MCRYPT_DECRYPT), "\x00"); var_dump($dec); //string(2) "42" 

This will not give you a base number of 32; it will provide you with encrypted data with each byte converted to base 16 (i.e. the conversion is global). If you really need to, you can trivially convert this to base 10, and then to base 32 with any library that supports large integers.

+1
source share

You can preliminarily determine 4-character codes (all possible combinations), then randomize this list and save it in this random order in the data table. When you want a new value, just grab the first top and remove it from the list. It is fast, without the need for on-the-fly calculation and guarantees end-user pseudo-randomness.

0
source share

Hashids is an open source library that generates short, unique, unclassified, YouTube-like identifiers from one or many numbers. You can think of it as an algorithm for obfuscating numbers .

It converts numbers like 347 into strings such as "yr8", or an array like [27, 986] into "3kTMd". You can also decode these identifiers. This is useful for combining several parameters into one or simply using them as short UIDs.

Use it when you do not want to disclose your ids database to the user.

It allows you to customize the alphabet, as well as salt, so the identifiers are unique only to you.

Incremental input is distorted to remain irrefutable.

There are no collisions, because the method is based on converting an integer to hexadecimal.

It was written with the goal of placing the generated identifiers in visible places, such as URLs. Thus, the algorithm avoids generating the most common English curse words.

Code example

 $hashids = new Hashids(); $id = $hashids->encode(1, 2, 3); // o2fXhV $numbers = $hashids->decode($id); // [1, 2, 3] 
0
source share

I finished creating the sum of the md5 identifier, using the first 4 of its alphanumeric characters, and if it's a duplicate, just increase the length until it no longer becomes a duplicate.

 function idToTinyurl($id) { $md5 = md5($id); for ($i = 4; $i < strlen($md5); $i++) { $possibleTinyurl = substr($md5, 0, $i); $res = mysql_query("SELECT id FROM tabke WHERE tinyurl='".$possibleTinyurl."' LIMIT 1"); if (mysql_num_rows($res) == 0) return $possibleTinyurl; } return $md5; } 

Accepted the redial answer, as it led me to this strategy.

-one
source share

All Articles