Issue 77 August 20 2009 | ||||
The Shortest Route Have you ever had trouble with web addresses that seems like they go on forever with thousands of fields and parameters? Have you ever tried pasting a Google Maps URL to a friend? Have you ever been caught trying to get a short URL to put on twitter? Well, welcome to the club. There are some wonderful web applications out there but the urls they use are impossible to remember. That's why recently, many URL shortening services have appeared. These services basically pick a long URL and create a short one that is more manageable. When you go to the short URL it redirects to the long one, it is as if you had typed those infinite long URLs all by yourself. Now, how much work would it be to create a simple URL shortening service in Revolution? The answer is 38. Creating the HTML interface This is a service that matches long URLs to short ones. It will generate short URLs like http://myServer/EDQN4J where EDQN4J is a code that represents the long URL. First question is how will we store those relationships? One might consider using XML or MySQL to do this job but since we’re creating a really simple service, let us do it with dumb plain text files. In our system each of those codes (eg: EDQN4J) will be a text file on the server. The content of this text file will be the long URL, so a file like EDQN4J might contain http://andregarzia.on-rev.com/cms.irev (which is not a long URL by the way but serves as an example). To make things more tidy, we’ll store all those text files inside a folder named “urls”, below, is a shot from my setup with some files in it already. Now let us create the form and code to save those text files. The form is a simple one, we just need one field, the long URL, and one button that submits the form. No other data is needed to perform our service duties. So the form looks like: <form method="post" action="index.irev"> Very simple and straight forward, right? Let us look at the code that saves the data submited by this form. Keep in mind that this form is using the post method, so all fields sent by this form to the server will appear in the $_POST array. We can simply check for the presence of $_POST[“url”], if this field is not empty then we generate the code for the small URL and save the text file. if $_POST["url"] is not empty then The code is dead simple, we pick the value passed from the form, generate a code for it and save it as a text file. So that deals with the user case one we specified above: a user comes to our service and has a long URL and wants a short one. That is done. Let us look how to pick the short one now and redirect back to the long. There and Back Again Let us review what we need now. The user has a code like EDQN4J and wants to be redirected to the long URL it represents. One might want to code another form, so the user enters the code and he is redirected, but this adds an extra step to the task of being redirected and spoils the purpose which is having short URLs that work just like the long ones. So let us just pass the code in the URL itself as a parameter like: http://myServer/index.irev?code=EDQN4J By trying to access this URL the user should simply and transparently be redirected. if $_GET["code"] is not empty then What is happening here? First we check if a code was passed in the URL, if so, we pick the value from its file and use the put header command to redirect the user, we also exit to top to prevent the rest of the HTML interface from appearing. Right, we’ve just created a fancy web 2.0 like URL shortening service. Let us put the full code in here: <?rev ?> And that's all folks, a simple thing to implement but it is really useful. In how many languages could you implement a full useful web application in 38 lines? Not many I believe. This system is online at: http://andregarzia.on-rev.com/haggisurl/index.irev in case anyone wants to give it a try. For example, try to access http://andregarzia.on-rev.com/haggisurl/index.irev?code=ECJ3VL This will redirect you to a enormously long Google Maps address pointing to the Dynamic Earth where in couple weeks we will have our RunRevLive conference. See you all there!
|
|