PHP Classes

URL Shortener: Create and expand short URLs in a MySQL database

Recommend this page to a friend!
  Info   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 381 All time: 6,688 This week: 151Up
Version License PHP version Categories
url-shortener 1.0.0GNU General Publi...5.0HTTP, PHP 5, Databases
Description Author

This class can create and expand short URLs stored in a MySQL database.

It can create a MySQL database table record for a short URL key that will be associated to a given long URL.

The class can also search for a given short URL key and return the associated long URL.

Picture of Ovunc Tukenmez
  Performance   Level  
Name: Ovunc Tukenmez <contact>
Classes: 14 packages by
Country: Turkey Turkey
Age: 36
All time rank: 72511 in Turkey Turkey
Week rank: 558 Up13 in Turkey Turkey Down
Innovation award
Innovation award
Nominee: 6x


- First, create a new MYSQL database. (e.g. 'linkdb')

- Second, create database table using the following SQL statement:

CREATE TABLE shortLinks (
id int(11) UNSIGNED NOT NULL auto_increment,
ulid varchar(15) NOT NULL default '',
longLink varchar(255) NOT NULL default '',
) TYPE=MyISAM COMMENT='Link Table' CHARSET=utf8 COLLATE=utf8_bin;

- Third, open UrlShortener.php class file and enter your details to following private properties. 

private $dbHost = 'localhost';
private $dbUser = 'db_user';
private $dbPassword = 'db_password';
private $dbName = 'linkdb';
private $dbTable = 'shortLinks';
private $baseUrl = 'http://localhost'; //shouldn't end with /


1) To get a long link from the database, you need to specify link_id to getLongUrl() function.


$link_id = 'z';
$urlShortener = new UrlShortener();
$long_url = $urlShortener->getLongUrl($link_id);

2) To create short link, you need to specify long_link to getShortUrl() function. 


$long_link = '';
$urlShortener = new UrlShortener();
$short_link = $urlShortener->getShortUrl($long_link);

3) To get only the link_id, you need to call getUrlId() function with long_link parameter.


$long_link = '';
$urlShortener = new UrlShortener();
$url_id = $urlShortener->getUrlId($long_link);


Sample script contains following files:

  Files folder image Files  
File Role Description
Files folder imageclass (1 file)
Accessible without login Plain text file DatabaseTableStructure.txt Data database table structure
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file index.php Example sample_script - index.php
Accessible without login Plain text file link.php Example sample_script - link.php
Accessible without login Plain text file .htaccess Data sample_script - .htaccess

  Files folder image Files  /  class  
File Role Description
  Plain text file UrlShortener.php Class class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:6,688
This week:151Up
For more information send a message to info at phpclasses dot org.