Skip to content

alesinicio/shrtnr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

shrtnr

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.

Shortened links types

Shrtnr allows you to use automatically generated sequential/random URL for each link that is shortened or to create your own customized links.

No interface

Shrtnr does not have any graphical interface. It uses simples API calls to insert, remove and access links.

MySQL database

Shrtnr uses a MySQL database to store the links. You should have access to one in order to make this work.

Functionalities

Shrtnr allows some configurations:
  • 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.

Installation

  • 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.

API calls

To include a new link with automatic shortened link generation, just use the following URL:
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).

Password protection

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.

GET or POST

Shrtnr works with GET or POST calls for inserting and removing links, your choice. Just configure it in the "config.php" file. Of course, if you choose to use POST, there's no need to include all that ugly data in the URL, just pass the arguments over POST.

Styling

Just put a `shrtnr.css` file on the same folder and style-away!

Feel free to change

If you like shrtnr but thinks it could be better, or want to change it, no problem. Fork the project, branch it away! If possible, leave a note. If it is something cool, I'd like to know and use it too!

LAST UPDATES

v1.02a

  • 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.

v1.02

  • Possibility to create random shortened links, instead of sequential ones.

v1.01

  • Redirect type (permanent or temporary) is now configurable;
  • Possibility to create single-use links.

About

Simple PHP URL shortener that uses two single files to achieve all functionality

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages