Skip to content

teamcfadvance/IrreversibleURLShortener

Repository files navigation

IrreversibleURLShortener

Provides functions for generating an irreversible key of variable length to be used as a shortened URL variable. In response to a question in the TACFUG google group, I have created this project to demonstrate a few ways in which to generate shortened URL variables to use in place of longer (e.g. CreateUUID()) variables.

This package relies on having a database table with a column that stores the generated keys produced by this code. As such, this package includes two CFC's:

  • Datasource.cfc
  • IrreversibleURLShortener.cfc

The Datasource.cfc is consumed (required) by the IrreversibleURLShortener.cfc.


To use, first initialize the datasource model/bean and the IrrversibleURLShortener component:

<cfset db = CreateObject('component','Datasource').init(
  dsn       = 'mydsn', 
  username  = 'myusername', 
  password  = 'mypassword') 
/>
<cfset urlShortMethod = CreateObject('component','IrreversibleURLShortener').init(datasource = db) />

Then, call the method passing in the [table] and [column] that stores the key to check the existence of in the database, optionally specifying the length of the generated key (6 chars by default) using one of the available methods (hash, aplha, alphanum or numeric, 'hash' by default):

<cfset myShortUrl = urlShortMethod.getShortURL(
  table     = 'mytable',
  column    = 'mycolumn',
  keyLength = myNumericKeyLength,
  method    = 'hash'
) />

OR

<cfset myShortUrl = urlShortMethod.getShortURL(
  table     = 'mytable',
  column    = 'mycolumn',
  keyLength = myNumericKeyLength,
  method    = 'alpha'
) />

OR

<cfset myShortUrl = urlShortMethod.getShortURL(
  table     = 'mytable',
  column    = 'mycolumn',
  keyLength = myNumericKeyLength,
  method    = 'alphanum'
) />

OR

<cfset myShortUrl = urlShortMethod.getShortURL(
  table     = 'mytable',
  column    = 'mycolumn',
  keyLength = myNumericKeyLength,
  method    = 'numeric'
) />

DEMO

This package now also includes a demonstration index.cfm, an Application.cfc where default values are defined, and a MySQL script to generate the demo table used.

You can also view a live demo of this code

About

Provides functions for generating an irreversible key of variable length to be used as a shortened URL variable.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published