Sponsor
Now Playing
- Alix Perez – I'm Free 2 hours ago
- Alix Perez – Intersections 2 hours ago
- Alix Perez – Forsaken 2 hours ago
- Glen E Ston – Ouroboros (Original Mix) 3 hours ago
- Black Sun Empire – Everything 3 hours ago
CakePHP URL Shortener Service Tutorial
August 26, 2009,
3,871 views
If you have just woken up from a 10 year long coma, you might realise that the internet has been dumbed down to 140 characters and services that offer “URL Shortening” are quite widely used. Well today, coma awakened or not, I will be writing out a basic tutorial which will give you an understanding of just how to create a service like bit.ly or owl.ly to shorten URLs so you can insert them to fit within your 140 chars max tweets.
In this tutorial I will be creating this website using CakePHP 1.2.4, because it rocks, and you will think it rocks too if you haven’t used it yet! If you are unfamiliar with CakePHP please jump into it now by checking out either of these two books: Beginning CakePHP by Apress & Free CakePHP Book by Matt Curry! So let’s begin!
Now this isn’t going to be a tutorial into all the joys and wonders of CakePHP. There are simply too many to list, my database wouldn’t be able to hold so much information. However if you are new to CakePHP, all that this tutorial requires is that you have successfully set up CakePHP, wherever you may prefer. There are plenty of tutorials and guides on how to get CakePHP set up on the web. I did a quick search and found this one to be quite good, but there are plenty more out there. The most common newbie gotcha I find is making sure the .htaccess files are uploaded correctly. But with all that said and done! Let’s get going with our URL shortening website!
Final Source Files: app.tar.gz
Images & CSS: imagescss.tar.gz
Demo: http://jotlab.com/tutorials/url/
*please note that the random string generation will lead to collisions. This is a beginner to intermediate level tutorial, so teaching about base converting is out of the question! Well for me!*
10 Comments
i have never seen such a comprehensive tutortial on this , good on yah, i’ll buy you a paypal beer any day
great work, thank you!
Awesome tutorial man! Thanks allot
nice post..what about stat a.k.a link tracking? it will be great if u include it.
i bake mine at letsclick.co.cc for learning purpose.
That would be quite easy, just depends how much information you would like to store.
For example you could simply increment a count field in the row for that entry.
Or you could make a completely new table in the database that would log each view/click and the according information, like IP, datetime etc.
The demo file not work http://jotlab.com/tutorials/url/, url not found
Hi, i don like very much the function getFilename. Why dont you try using a function the transform the id of the link into a string with numbers and letters?
pseudocode for add a link;
// find link, if not in database insert new link
// link_shorted= base_convert($link_id, 10, 36);
// return link_shorted
pseudocode for get a encoded link;
// link_id= base_convert($link_code, 36,10);
// search link by id
// redirect
if you want even shorter link you could use this functions;
function any2dec( $num) {
$base=62;
$index = substr( “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”, 0, $base );
$out = 0;
$len = strlen( $num ) – 1;
for ( $t = 0; $t = 0; $t– ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num – ( $a * pow( $base, $t ) );
}
return $out;
}
Exactly right. This is why at the beginning I said it wasn’t the best way to do it. But for the sake of this intermediate tutorial i thought using those baseencodes would be far beyond the scope of the tutorial. I did find similar functions on php.net that would be used to encode/decode ids or unique identifiers. But thanks for your observant comments!
>>Exactly right. This is why at the beginning I said it wasn’t the best way to do it.
upss, i didnt read anything i just read the code, my bad!
No problem at all! Thanks for your input
5 Trackbacks
[...] CakePHP URL Shortener Service Tutorial • Jotlab [...]
[...] Jotlab has a post on going from clean install to working URL shortener in 8 simple pages. [...]
[...] CakePHP URL Shortener Service Tutorial [...]
[...] CakePHP URL Shortener Service Tutorial [...]
[...] 2010 por eugenio85 Etiquetado: acortador, cakephp, PHP, tutorial, url shortener Despues de leer este articulo decidi escribir mi propio tutorial, mientras pensaba en como escribir note que en meneame utilizan [...]