URL shortener, base convert and cURL
Weekends always give me some time to work on my own projects, and today I decided to create an URL shortener at wr0.org. I had written one before, so after an hour it was up and running. It basically works by inserting the long URL into a database, and then converting the assigned ID to base 36.
Base convert
Humans normally work with a base 10 system, we use 10 digits for counting: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. When we reach the end of our 10 digits, we append another and continue counting. Computers work with a base 2 system, using the digits 0 and 1. To write the number twentyfive in base 10 we write "25", in a base 2 system this would be written as "11001". In base 36 you generally use the letters a - z as extra digits, and then the number twentyfive would give us "p".
This is quite useful when trying to make a URL shortener, because you want to be able to shorten a lot of URLs, and they all need to be given an unique ID. When we assign IDs using a binary system, we run out of single digit IDs after shortening 2 URLs. The "normal" base 10 system allows for 10 single digit IDs and 36 single digit IDs can be assigned using base 36. This seems like much effort for little reward, but it gets better, take a look at this table.
| Binary system | Base 10 system | Base 36 system | |
|---|---|---|---|
| Single digit IDs | 2 | 10 | 36 |
| Double digit IDs | 4 | 100 | 1296 |
| Three digit IDs | 8 | 1000 | 46656 |
| Four digit IDs | 16 | 10000 | 1679616 |
As you can see there are 46 times as many 3 digit IDs in a base 36 system then there are in base 10, and a staggering 168 times as many 4 digit IDs!
cURL
Then I wanted a "tweet this" button under every post here on wesselrossing.nl. Even though the links to posts are not that long themselves, I wanted them to be shortened automatically through wr0.org. Since it is not possible to do AJAX requests between different domains, and having heard a lot about cURL but never actually used it, this seemed like a good moment to give cURL a go.
cURL stands for Client URL Library and allows you to connect and communicate to many different types of servers with many different types of protocols. Since there are many good tutorials on cURL out there I won't try to add to them, instead I will explain how I used it.
As you can see there is a button underneath this post that says "Tweet this!", when you click this button your browser will request a PHP script that in his turn will request a PHP script on the wr0.org server via cURL. The most part of it happens in the following lines of code.
$ch = curl_init('http://wr0.org');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,'return_link_only=true&long=http://wesselrossing.nl/everything.php?id=' . $_GET['id']);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$tiny = curl_exec($ch);
redie('http://twitter.com/home?status=' . urlencode('I am reading "' . $_GET['title'] . '" by @wesselrossing, check it out: ' . $tiny));
If you would like to use wr0.org to shorten URLs with PHP too, you can just copy the above script and edit it for your needs. By the way, you can also just browse to wr0.org if you want to manually shorten a URL.