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 enough user ratingsTotal: 386 All time: 6,687 This week: 455Up
Version License PHP version Categories
url-shortener 1.0.0GNU General Publi...5.0HTTP, PHP 5, Databases


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: 38
All time rank: 71911 in Turkey Turkey
Week rank: 286 Up6 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 6x


----------------------------------------- INSTALLATION ----------------------------------------- - 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 '', PRIMARY KEY(id), UNIQUE(ulid), KEY(longLink) ) 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 / ----------------------------------------- CLASS USAGE ----------------------------------------- 1) To get a long link from the database, you need to specify link_id to getLongUrl() function. Example: $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. Example: $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. Example: $long_link = ''; $urlShortener = new UrlShortener(); $url_id = $urlShortener->getUrlId($long_link); ----------------------------------------- SAMPLE SCRIPT ----------------------------------------- Sample script contains following files: index.php link.php class/UrlShortener.php .htaccess

  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,687
This week:455Up