Simple PHP URL shortener that uses two single files to achieve all functionality.
The shrtnr project provides a very simple way for people to build their own URL shorteners using just two files (plus a .htaccess to provide REST functionality).
The idea is to get long and complex URLs and turn them into small addresses that can be easily distributed. For example, you can turn http://www.complexdomain.com/waaaaytoolongurltoremember
into something like http://yourdomain.com/simple
.
- Optional password protection for inserting links.
- Optional password protection for removing links.
- Shrtnr automatically forbids two equal custom URLs.
- Configurable length and allowed characters for new automatically shortened links.
- Links may be user-defined, sequential or completely random.
- Global "toggle switch" for completely disallowing link removal.
- Configurable "error page" if user tries to access an invalid link.
- Consider shrtnr should be deployed to "http://yourdomain.com".
- Change the "config.php" file to match your settings. All settings are explained within the file.
- Copy the two ".php" files to your root domain folder.
- Copy the .htaccess to your server according to our example, or change yours to match our example. IF YOU HAVE MORE STUFF IN THE FOLDER WHERE YOU ARE COPYING THE .htaccess FILE, BE CAREFUL!
- Access "http://yourdomain.com?install" and shrtnr will create the tables needed on the database. You should have the database already created, though.
http://yourdomain.com/?include&url=http://www.yourlink.com.br
To include a new link with your custom shortened URL, just use the following URL:
http://yourdomain.com/?include&url=http://www.yourlink.com.br&customURL=coolURL
To remove an existing link from the database, just use the following URL:
http://yourdomain.com/?remove&link=notsocoolURL
To list all entries in JSON format, just use the following URL:
http://yourdomain.com/?listLinks
To list all entries in a formatted HTML table, just use the following URL:
http://yourdomain.com/?listLinksHTML
Shrtnr will always respond with a JSON string, with exception of the method "listLinksHTML", which will return pure HTML. The first field will always be "status", and should be 0 (error) or 1 (success). If an error occurs, shrtnr will output a second field "error" telling you why things went wrong. If you insert a link successfully, shtrnr returns a field "shrtnURL" with the shortened URL (either the automatically generated URL or your custom one).
Shrtnr allows you to protect the creation, deletion and listing of links. When this option is toggled on, a dynamic hash should be included in every request. The hash is made of a salt (which is provided in the "config.php" file) and some information of the request itself. The calculation is made this way:hash = md5(salt + url + link + customURL + timestamp)
The parameters "url", "link", "customURL" and "timestamp" may be empty depending on your request type. No problem.
With the hash in hands, just append a "pwd=hashyoucalculated" parameter in your call. For example (the hash below is valid for salt '123456', if you are wondering):
http://yourdomain.com/?include&url=http://mycoolwebsite.com&customURL=mcweb&pwd=7a9532cff367e019fca9ef9575dd3f3f
Specifically on the "listLinks" and "listLinksHTML" methods, you should inform an additional timestamp=yyyymmddhhiiss
with your current time. Shrtnr will check if your timestamp is within a 10 minute window of the server's own time to validate the request.
- Bugfix - shrtnr.css file was not being included due to .htaccess misconfiguration;
- Bugfix - listing links in HTML mode generated table with missing header for "single use" option;
- Update - config.php has new default values and revised documentation.
- Possibility to create random shortened links, instead of sequential ones.
- Redirect type (permanent or temporary) is now configurable;
- Possibility to create single-use links.