My Ride
Started: Tuesday 18th June 2013 7:23am
Distance: 12.19km
Duration: 00:27:41
Rest Time: 00:08:53
Climb: 77m
Max Speed: 43.2kmph
Average Speed: 26.43kmphInstagrams
-
Recent Posts
Recent Comments
Archives
- February 2013
- December 2012
- September 2012
- July 2012
- January 2012
- September 2011
- August 2011
- February 2011
- January 2011
- November 2010
- October 2010
- August 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
Categories
Meta
CakePHP URL Shortener Service Tutorial
August 26, 2009,
19,300 views
We now have an awesome page set up. Let’s add some logic to it! Open up your model and controller files and let’s get going! First we’ll pump up our model to generate and test both the md5 of the url and the unique random code associated with the shortened url. So open up the model file, shorten.php and add:
function getFilename($length=8, $prefix=''){
$string = "";
$possible = "abcdefghijklmnopqrstuvwxyz0123456789";
$i = 0;
do {
while ($i < $length) {
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
if(rand(0,1)){
$char = strtoupper($char);
}
$string .= $char;
$i++;
}
} while (file_exists(sprintf($prefix, $string)) == true);
return $string;
}
What does this do? Well it generates random strings, of both lower and upper cases from the alphabet. It also checks to see if the code exists already in the database, and if it does, to generate another one, until it finds one that is aok to use! This function will come in handy later on when we actually insert the url record into the database. Next let’s write a function that md5′s the url if it doesn’t exist already, and if it does, to tell us the url exists already:
function md5Url($url){
$md5edurl['url'] = md5($url);
$md5search = $this->find('count', array('conditions'=>array('unique'=>$md5edurl['url'])), array('recursive'=>-1));
if($md5search > 0){
$md5edurl['exists'] = false;
} else {
$md5edurl['exists'] = true;
}
return $md5edurl;
}
False? why return false? Well let’s think about this for a moment. Think about what we want to do if the url already exists. We don’t want to save it again! So in effect, we return false that the url should not be re-saved. Instead it should be reused. This function is easy enough to understand. MD5 the url, then see if one exists in the database already. We store whether the url exists already in the exists element of the $md5edurl alongside the md5′d url!
Both of these functions however use a CakePHP function. The find command. It firstly has the find type, in our case both are counts, the conditions for its look up. However the last option might puzzle you a bit. Recursive is set to -1. This is if we had any table associations in place, we wouldn’t need them in these two look ups. CakePHP would automatically include joins on these tables, including data which we don’t need. Setting this recursive to -1 will lock CakePHP’s find to only that table! Easy!






14 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
Hi all!
Thanks for that great tutorials. the thing that make me a bit uncomfortable is what will the effect of making url shortened on the site in the term of speed. and talking about dynamic links how will you get shortcode will model association useful in that case. one more question in the routes.php you have write
Router::connect(‘/:shortcode’, array(‘controller’ => ‘shorten’, ‘action’ => ‘redirectUrl’), array(‘pass’=>array(‘shortcode’)));
it will redirect whenever you write anything after ‘/’ for rss feed and xml link will it don’t cause problem?
Hey man … everytime when i am going to open your page my virus protection pops up and displays an exploit : Blackhole Exploit Kit (type 2170) which comes from http://www.jotlab.com/index.php?ak_action=aktt_jsv=2.4
and second one from
http://www.jotlab.com/index.php?ak_action=aktt_cssv=2.4
more information on:
http://www.webopedia.com/TERM/B/blackhole_exploit_kit.html
Cheers
@kapil – Sorry the first part I do not understand your english. The second question is true. It will capture all urls with /url. But if you don’t want that, put your urls above that and use shortcode as the last catchall.
Hi there, I read your blog on a regular basis. Your humoristic style is witty,
keep up the good work!
8 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 [...]
[...] sidebar */ google_ad_slot = "1113860990"; google_ad_width = 300; google_ad_height = 250; 1. CakePHP URL Shortener Service Tutorial by JotLabIn this tutorial I will be creating this website using CakePHP 1.2.4, because it rocks, [...]
[...] View The Tutorial : CakePHP URL Shortener Service Tutorial [...]
[...] Cómo hacer un acortador de URL’s con CakePHP. [...]