PHP Classes
elePHPant
Icontem

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    
Last Updated Ratings Unique User Downloads Download Rankings
2012-11-05 (7 years ago) RSS 2.0 feedNot yet rated by the usersTotal: 376 All time: 6,633 This week: 391Up
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.

  Performance   Level  
Name: Ovunc Tukenmez <contact>
Classes: 13 packages by
Country: Turkey Turkey
Innovation award
Innovation award
Nominee: 5x

 

Details
-----------------------------------------
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 = 'http://www.site.com';
$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 = 'http://www.site.com';
$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  
 0%
Total:376
This week:0
All time:6,633
This week:391Up