Quick Tip: Create an Easy LINK Shortener With 10 Lines of PHP

Quick Tip: Create an Easy LINK Shortener With 10 Lines of PHP

I love to resolve problems with small, one-off scripts. The fewer lines that I have to write, the better. Lately I had an additional opportunity to do simply that. I needed a means to embed web links in PDF records, the locations of which I may require to update in the future. Links in PDFs can’t be updated without generating and sending out the documents once again, which is time consuming. The links were additionally long, and also it would certainly be tough to type them by hand if the documents are published.

This problem can be solved well with an URL shortener. Such services have actually been around for a long period of time, however there are a couple of issues with them:

You depend upon an outside service – when it is down, all your links are down. Also worse – if it goes out of service, so do your web links.
All clicks are being monitored for unidentified objectives.
You generally can’t change the location of a short web link.
Some services permit you to choose a custom-made slug, yet all the great ones have currently been taken.
The stage is established for creating a quick as well as unclean PHP manuscript!

The Idea
If we are to create a link reducing solution that you are the only customer of, we may leave out things like user registrations as well as admin panels. Here are some of the functions of our web link shortener:

Short web links will certainly be available in the type http://example.com/l/short-link. Seeing this URL will certainly reroute the browser to the genuine address.
The entire manuscript will certainly be held in a single php data – index.php, without outside dependencies.
There will not be an administration display for including or modifying web links. Every little thing will be dealt with by a simple text documents that remains on your server and also which you can edit easily.
There will not be any kind of automatically designated ids to web links – you will pick the brief slug yourself.
These demands considerably streamline the manuscript that we will be creating. To make points even much more straightforward, I made a decision to keep the links in an ini data, due to the fact that it is extremely simple to modify by hand, and also it has native assistance in PHP using the parse_ini_file function (this will conserve us from having to read the materials of the file and analyzing it ourselves; and as an incentive this feature is really fast).

 

The INI File

Here is what the ini file looks like:

links.ini

 
google = https://www.google.com/
fb  = https://www.facebook.com/
 
 Short link on the left, long link on the right. It doesn't get any simpler than that!

The Code

The PHP script is equally bare bones:

index.php

$links = parse_ini_file('links.ini');

if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){
    header('Location: ' . $links[$_GET['l']]);
}
else{
    header('HTTP/1.0 404 Not Found');
    echo 'Unknown link.';
}

The script expects to receive the slug in $_GET['l'] like so: http://example.com/index.php?l=google. This obviously is not very short, but we can make it prettier with an .htaccess file (assuming you are running the Apache webserver).

.htaccess

RewriteEngine On

RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?l=$1 [L]

This file should sit in the same folder, next to index.php and links.ini. What it does, is to route every request for a file that is not index.php, to index.php?l=xxx. This prevents infinite loops and also makes links.ini unreachable from a browser.

For the best result, place these three files in a folder with a short name like ‘l’ in the root of your website, and you will get short links like http://example.com/l/google(Obviously google.com is a poor choice in this case – the short link is longer than the original!)

We’re Done!

If you don’t want to keep a separate file with links, you might eliminate it entirely by placing the short links directly in the script as an associative array. Another improvement might be to build a simple administrative interface that changes the contents of the ini file. I am sure that there even more awesome things that you can do with this code. I hope that you find this tiny script useful!

Leave a Comment