fcOnTheWeb Logo Web technologies, made easy.

fcOnTheWeb Red DotHow to make a long URL shorter

TinyURL is a great, service offering shorter URLs that are much more portable.

The concept is simple: the user enters a long URL - the sort you might get from a shopping cart or similar, that has a variety of variables set at the end - and TinyURL then gives you a 25 character URL in its place. This is obviously much more user friendly. You simply enter this new, shorter URL into the browser, and you are directed to the original URL. Easy.

So, at fcOnTheWeb, we have created our own version.

It's quite simple really. The submitted long URL is written to the database with an accompanying, random hash value. The new, shorter URL is then returned to the user for them to take in place of the original URL.

Here's the algorithm we use to create the random hash:

$hash_is_verified = false;

while ($hash_is_verified == false) {

$random_hash = get_random_hash();

if (check_random_hash_in_db($random_hash)) {

 

} else {

$hash_is_verified = true;

}

}

...

function get_random_hash() {

$random_hash = "";

$letters_array = array("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

for ($i = 0; $i < 7; $i = $i + 1) {

$random_hash = $random_hash . $letters_array[array_rand($letters_array)];

}

return $random_hash;

}

The URL is run through the first section of code, which generates a random string for it. Each time a random string is returned, it's checked to see if it is present in the database (check_random_hash_in_db()). If it is, we generate another random string. This continues over and over until we generate a unique string. That string (hash) is then entered into the database, along with the original URL and the other necessary details.

When generating the random string, there are a couple of things to be considered. We thought about using a sequential system, such as aaaaaaa, aaaaaaab, aaaaaaac, etc. but that would then make it very easy for people to enter random hashes and access URLs that had been submitted by other people. That was the main factor in deciding to use a random system. We used a mixture of upper case and lower case letters and numbers to give a large number of possible hashes. However, some characters are missing - we removed 'similar' characters (such as O and 0, I and l) so as to make the humanly readable hashes more user friendly. The current combination has 21,083,232,519,264,920,575 possible combinations.

When the new, shorter URL is accessed (entered into the browser), the database finds the original URL related to the hash, and the user is then directed to that page. If the submitted URL doesn't begin with an http, then one is appended to it.

function validate_url($provided_url) {

if (strpos($provided_url, "http") > -1) {

return $provided_url;

} else {

return "http://" . $provided_url;

}

}

...

header("Location: " . validate_url($long_url));

Simple really. You can try it for yourself, by entering a URL below. You will then be provided with a short URL, which you can preview, or copy. When entered back into your browser, you will be directed to the original URL.

Unfortunately, the length of fcOnTheWeb.com (14 characters) tends to make URLs a bit longer than the convenient TinyURL.com (11 characters), and the "http://" further adds to it, but you understand the point of the exercise.

While this service works well here, there is a more accessible version available for you to use, at http://fcOnTheWeb.com/url.php. This service will remain in place indefinitely, so you can use it as you wish.

Enjoy, and happy URL shortening.

Long URL:

ferrari_chris


Add your comment on this article below:

Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.

Form submitting now...

Name:

Website:

Email address (not displayed):

Enter your comment below: