Url shortening algorithm

Hello Intelligent,

Please help me with this.

what can be the simple logic to generate 6 characters short url ( Should contain a-z, A-Z, 0-9 - Total 62 Chars) for a long url without any repeated result.

This should be of interest. :slight_smile:

thank you anthony

Why not just base-62-encode str_replace(‘.’,‘’,microtime())?

Thanks from me too, i’d been looking for something like this.

I’ve been thinking about this since yesterday.

I guess if you enter domain.com/xunrfg you’re going to get a 404 right? So presumably you need to redirect all 404s to the index.php file and have that echo a location in the header to redirect the user.

Thanks all for helping

Actually you’ll use mod_rewrite to redirect all requests to the domain (except perhaps js, images, css, and other media) to index.php in which a function should resolve the shortened URL to the full url. Now you don’t have to redirect to the full url. Instead, you can set index.php to load specific php files, function, classes, and templates (or controller and action) depending on the values provided in the full url.

You can use any random string generation function (search on google). After generating the string, be sure to check your database to see if the code is already in use. some may say 62 ^ 6 = 56 800 235 584 is not random enough.

And if your string already exists, you’re wasting time.
Better to use something that will never be the same (time/microtime) twice. Yes, it’s not actually ‘random’. Note that the OP didnt ask for random. He asked for a UUID, ala [FPHP]uniqid[/FPHP]

The problem with uniqid StarLion, is that its too long. Anthonys method is still unique because its based on the insert id of an auto incrementing table and therefore the number of the latest row will always be different to the last row and hence the code will be too.

Thanks friends

A simple solution can be an “auto incrementing” id or “microtime”.

But one more thing I have to ask if above said methods used. If I provide a custom url recommendation

Eg :
Long url : http://www.url.com/123/abc/
Short url (Auto) : http://abc.xy/eGe323
Short url (Manual Recommended) : http://abc.xy/manualrecommedation

In the case there may some conflicts.

Same value of “X” microtime can be equal to the already added custom value by the user. This can be solved by adding 1 with the microtime.

I can check the value if already added or not by mysql query. But in case if many checks lead to result of “value already added by user”. I m wasting time. Am I right? So, What can be the solution.

My favoured approach is to build a large series of “random” short identifiers (or, shuffled sequential numbers). Then, whenever you need to use a new one, just ask your database or wherever for the next one (or, any one) in the series that isn’t being used.

There is absolutely no need for anything to happen on-the-fly like converting decimal integers (e.g. AUTO INCREMENT primary keys) into something shorter, nor for the wasteful process of generating a usable random short identifier by polling to the database to see if it exists, and repeating until it does not.

This also helps with the possible collisions that may occur when allowing customised short identifiers: if your system already has a list of all (used or unused) identifiers, disallowing a custom choice that already exists in the list is trivial.

I cannot understand well. :frowning: Please can be explain briefly.

Perhaps you should provide an example of this.