A TinyURL PHP page
A TinyURL PHP page
Written by Harry Fairhead   
Sunday, 05 July 2009
Article Index
A TinyURL PHP page
Building the new URL

Building the new URL

At this point we have the current URL and have checked that it is valid and it's time to actually convert it. We could do this using string manipulation which isn't difficult, but since we have started using the Joomla framework we might as well continue to do so - it is more robust!

First create a new JURI object to hold the new URL:

$uriNew= new JURI($uri->base());

The new URL has been set to the same base, i.e. site, as the original URL by the parameter passed to the constructor. All that remains is to set the query string and we can do this in a two-step process. First we set up a name/value array containing the settings we want to use:


Notice that the itemid has been set to 2 - the reason is that all of the articles on the website in question that need to have tiny URLs are arrived at from menu 2. If you need a more complicated solution to the itemid problem you could code it within the tiny URL and add it to the finished URL.

Now we have the query string specified by the name/value pairs in the array we can use the Joomla supplied setQuery method to add it to the new URL:


That’s the job almost done. All we now have to do is use the PHP header comment to redirect the browser to the new URL:

header("Location: $sURL");

Of course the last two lines could be condensed into one but where's the fun in that?

The basic idea can be customised even further and with the help of some URL rewriting rules at the server you can even simplify the tinyURL.php?id to something even easier such as tURLid but that’s another story.

If you would like the code for this project ready to run then register with I Programmer and email code.



Last Updated ( Monday, 06 July 2009 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.