A URL Shortener
In January I was invited to apply to Upwork Pro which is described as a way for freelance programmers to more easily find quality work on Upwork (itself a job board of freelance projects). The application included a programming test. While I wasn’t accepted into the program (likely because I lack established freelance work history on Upwork) the programming test itself seemed like a something to toss into my GitHub repository in case a prospective employer wants to see more source code of mine. I figure I should also write up a little something on this programming project to create a URL shortener site.
The project was a timed test. A timer started once I was viewed the project requirements and I had to upload my source code within before three hours was up. I made the decision to develop this code using native PHP (no frameworks like CodeIgniter or Laravel) for several reasons. Since the code needed to be extremely portable so that the code reviewers could examine the source and, potentially, demo the website, a heavy framework like Laravel was undesirable. Also, the project did not appear to be complex enough to require an MVC framework at all. Therefore I chose to develop in native PHP. The code base could be cleaner and more organized using a framework, but time and size was of the essence.
The project had three primary requirements:
- Shorten a valid URL provided by the user
- Create a shortened URL specified by the user for a valid URL provided by the user
- Allow a user to look up a shortened URL
A bonus was given for additional requirements:
- Provide a mechanism for cleaning up old shortened URLs
- Using a configuration file for important settings
- Logging actions occurring with the site
Prior to this project I never put much thought into how URL shorteners determines the URL it generates. All I was sure of was that there was a short domain (such as http://go.gl) with a seemingly random set of characters. Not knowing how these URLs are generated by actual URL shortening services I came up with my own way. I borrowed it from a previous project that required generating a unique URL for unsubscribing from e-mail lists.
I take the full URL, as a string provided by the user, and get the SHA1 hash of it. I then take a subset of the hash based upon a length variable set in the configuration file. The domain of the URL shortener is also in the configuration file (http://up.up in this project's case). This allows the full URL of 'http://google.com/webmaster/tools' to be shortened to 'http://up.up/600dae50'.
There is a potential problem with this method of generating short URLs. There is the possibility that the selected sub-string of two SHA1 hashes could be identical. This will require some change to the shortened URL which is not currently implemented.
Anyhow, the source code is available on my GitHub account if you are interested in looking at it. I’ve done some minor cleanup since that initial three hours of coding and if I find myself interested in doing so I might implement the logging mechanism that was a bonus for the project.
Add new comment