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