Webmaster Blog

Web Design, Web Hosting, SEO, Graphic Design

Archive for the 'PHP' Category

22 October
3Comments

Create your own URL shortener

Why do we need URL shortening services?

URL text being squashed in a viceStudies have shown that if a URL is longer than 30 characters, many consumers are likely to be put off especially if faced with having to type it in manually. In addition to this, longer URLs are exposed to a greater risk of being input incorrectly. This can cause massive problems when sharing your information especially when sharing links via non-computer based methods such as printed text or via the telephone.

To combat this problem, URL shortening services were introduced. URL shortening services do exactly what they say on the tin; they convert long, complex URL strings into shorter more manageable ones. There are now hundreds if not thousands of free services available for this purpose.

One thing that struck us whilst using one of these services was just how simple it would be to recreate our own. We’re aware that there are many free scripts available to download that will provide URL shortening functionality, however, since it is so simple to recreate, it makes for a great learning exercise.

How do URL shorteners work?

For a demo, take a look at our URL shortening script.

So in order to create our own, we need to understand what is happening under the hood of a URL shortening service. You’d be forgiven for thinking that these services use some form of complex string compression and encryption, however, they actually work in a much simpler way. When the user enters their ‘long’ URL, the long link is added to a database and assigned a unique ID (the primary key). It is this primary key that is then used to retrieve the original long URL and redirect the user to that page.

Let’s use an example. Imagine we have the long URL, http://www.vermadesign.co.uk/blog/2009/10/create-your-own-url-shortener/ which we input into the URL shortener and submit. The URL is added to the database and assigned a unique ID. For arguments sake, I’ll assume this is a brand new database and therefore this URL is the first. The ID returned is therefore ’1′.

The short URL returned could therefore be something like http://www.vermadesign.co.uk/?1. You can see that this is a much shorter URL than the one before.

In order to retrieve the long URL from the short version, when a user visits the short URL, a script located at http://www.vermadesign.co.uk/ will check whether a parameter (in this example ’1′) has been declared (declared using ‘?’) and use this parameter to retrieve the long URL. This can easily be achieved with only several lines of code in PHP.

Before we proceed with developing the URL shortening scripts, we need to understand how to further reduce the URL length. You my have noticed many other URL shortening services have parameters that contain both letters and numbers. By using letters as well as numbers, we can further reduce the length of the shortened URLs.

First we need to understand how the code is generated . For this example, we will learn about Base 36. Our normal method of counting is done in denary (base 10) – that means that each digit has ten possibilities 0 to 9. By converting to a higher base, we reduce the number of digits required to represent the same number. Base 36 works in the same way as Base 10 in that it starts with 0-9 however, instead of moving to the next digit, the letters a-z can then be used i.e. x..y..z..10..11..12..13..14..15..16..17..18..19..1a..1b..etc.

So the number 1,000 can be represented as ‘qr’ in base 36.

27 x 36 = 972 (27th character in Base 36 is ‘q’)
28 x 1 = 28 (28th character in base 36 is ‘r’)
qr = 1000

As you can see, by switching to base 36, the 1,000th short link will actually be two characters shorter using this method.

There are many far more effective methods of reducing the short URL further, however, for simplicity, I would suggest starting with this method.

You may be wondering why I do not switch to a higher base 36 as it would further reduce the URL length. The reason I have opted for base 36 is because all the digits can be represented by either a letter or number and are therefore perfectly safe to use in a URL. If you were to use a very high base, some digits may be represented by special characters which in turn may cause issues with older computers or web browsers.

How to make your own URL shortening script

In order to proceed you need to ensure you have a server with MySQL and PHP installed. If you don’t, I recommend downloading an all-in-one test server such as XAMPP. This part of the tutorial assumes you have a basic knowledge of HTML forms, PHP and MySQL.

The first step is to create the database table:

1
2
3
4
5
6
7
CREATE TABLE IF NOT EXISTS 'url-shortener' (
  'url_id' int(10) NOT NULL auto_increment,
  'url' varchar(500) NOT NULL,
  PRIMARY KEY  ('url_id')
);
 
INSERT INTO url-shortener ("url") VALUES ("http://www.vermadesign.co.uk/blog/2009/10/create-your-own-url-shortener/");

The above MySQL statement will create a very basic table. You may want to consider increasing the length of the url field. I have opted for 500 in order to save space. The second part adds an example long URL so we can test the next script.

Next we need to work on the PHP redirect script. The function is very simple, the script will check whether a parameter has been set, convert from base 36 to 10, look up the long URL in the table and redirect to that URL. If no long URL is found, a ’404 Page not found’ error is returned.

The toughest part of this script seems to be converting between base 10 and 36, however, PHP has a very simple function for converting between bases called base_convert(). So putting it all together, we get the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Get short link code
$request = $_SERVER["QUERY_STRING"];
 
// Connect to MySQL database
 
// Convert short link parameter from base 36 to into the URL ID which is ase 10
$url_id = base_convert($request,36,10);
 
// Search the database for a long URL given the ID
$query = "SELECT * FROM url-shortener WHERE url_id='".$url_id."'";
$data = mysql_query($query);
 
// Count number of records matching the above query - will only ever be 0 or 1 as the ID is unique
$count = mysql_num_rows($data);
 
// if a long URL exists in the database given the ID - if $count equals 1 the below if statement returns true
if($count) {
	// Fetch MySQL data
	$info = mysql_fetch_array($data);
 
	// Close database connection
 
	// Redirect to long URL
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: ".$info['url']);
	exit();
} else {
	// Close database connection
 
	header("HTTP/1.0 404 Not Found");
 
	// Display 'page not found' page
	echo "404 Page not found";
	exit();
}

Once created, save as index.php and store in your domain’s root folder. You should then be able to see the script in action by typing http://www.mydomain.com/?1 (substituting mydomain.com for your domain name) into the address bar. Also try using a parameter of 2 or above and you should find a 404 page not found error is returned instead.

The last part is creating the form to input a URL to shorten and the script to add it to the database. Start by creating a simple form with a textbox named ‘url’ and a submit button like below:

?View Code HTML4STRICT
1
2
3
4
5
6
<form id="urlform" action="create-short-url.php" enctype="application/x-www-form-urlencoded" method="post">
 
URL:
<input id="url" name="url" type="text" />
<input class="button" name="submit" type="submit" value="Shorten" />
</form>

Next we’ll create the create-short-url.php script to process the form data and add the URL to the database. This script is much simpler than the last one, we need to gather the URL inserted into the form and add it the the database. The ID from the newly inserted URL is then converted into base 36 and the short URL is constructed and output to the user.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Collect form data
$url = $_POST['url'];
 
// Connect to MySQL database
 
// Add URL to database
$insert_query = "INSERT INTO url-shortener (url) VALUES ('".$url."')";
mysql_query($insert_query);
 
// Get the ID of the recently inserted URL
$url_id = mysql_insert_id();
}
 
// Close database connection
 
// Generate the base 36 short code from the URL ID
$short_code = base_convert($url_id,10,36);
 
// Display the newly formed short link ensuring you substitute mydomain.com for your domain name
echo "Short URL is http://mydomain.com/" . $short_code;
exit();

Put it all together and we have now finished our very basic URL shortening service.

Additional tasks

The above example shows you how to make a very simple URL shortener, however, many features have been left out including security measures to keep the explanation as simple as possible.

Below are some suggestions of how you could further develop and improve the above script.

  • Add additional fields in the database table so you can log the IP address of the URL submitter and date and time the URL was submitted. You could even set a field to dictate how long the short URL remains active
  • Validate the form to check the structure of URL before adding to database. This will help reduce the strain on the server and help to reduce broken links
  • Check if the URL already exists in the database and if so, return the existing short URL
  • Properly escape the URL to avoid SQL injection. This is very important for security
  • Edit your .htaccess and use mod_rewrite to remove the required ‘?’ from the short URLs as well as set the default domain to the non-www. version. Doing both of these will save 5 characters on the short URL

If you have any questions relating to this article or suggestions of how we can improve it, please feel to leave us a comment below and if you enjoyed reading it, please help us share by using the links below.