Source for file class.url_shortener.php
Documentation is available at class.url_shortener.php
* @author Julius Beckmann
* @link http://juliusbeckmann.de/classes/url_shortener/
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* class.url_shortener.php
* Copyright 2009 Julius Beckmann
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MySQL Table for this Class:
CREATE TABLE IF NOT EXISTS `url_redirect` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`url` varchar(255) NOT NULL,
`ts` int(10) unsigned NOT NULL,
`ip` int(10) unsigned NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=1000 ;
If you want to log number of hits, create such a table:
CREATE TABLE IF NOT EXISTS `url_hits` (
`id` BIGINT UNSIGNED NOT NULL ,
`hits` BIGINT UNSIGNED NOT NULL ,
This database layout is abled to store more then 4 Billion links.
This limit is set by the MySQL BIGINT collumn used.
The md5 collumn is used for validating and searching duplicate Urls.
I did a short test and inserted about 100.000 Links and Hits
which needed only ~17 MB of space. => 180 Byte per Link (with index and hits)
The only limitation by the class is the convert method.
After 1.000.000.000.000.000.000 links there would be a overflow.
* Class for shortening urls
* The urls will be stored in a MySQL database, read doku for table Format.
* @name Url shorten class
* @version v0.1_2009.09.10
* Name of Database table for storing urls and keys
* Name of Database table for storing redirect hits
* Convertion basis for the key convert method.
* Default and maximum is 36 (36 => 0..9 and a..z)
* If you want to have hex keys set this to 16 (16 => 0..9 and a..f)
* Regex for URL validation
* This one is very simple and does only allow urls
* starting with http:// and https://
* List of forbidden URL parts
* Can be used to avoid making short links from short links
// --- PUBLIC Methods ---
// public means: You can _safely_ use this functions from outside.
* Method for creating a new redirect
* Will return data from database if url already shortened
* @param string $url Url to shorten
* @return array Array with all available data inside, or empty one
// Check if url is already in database
* Method for selecting a redirect from database
* @param string $key Redirect key to search
* @return array Array with all available data inside, or empty one
* Logs a redirect hit by key
* @param string $key Key of hitted link
* @return bool false on error
// We will use LOW_PRIORITY/DELAYED here to smoothen the database IO.
$sql = 'UPDATE LOW_PRIORITY '. $this->db_table_hits. ' SET hits=hits+1
WHERE id='.(int) $id. ' LIMIT 1;';
// Insert if update failed
. ' (id,hits)VALUES(\''.(int) $id. '\',\'1\');';
* Simply returns the number of current hits for this key
* @param string $key Key of hitted link
* @return int|falsefalse on error
* Simply returns the number of current urls
* @return int|falsefalse on error
// --- PRIVATE Methods ---
// "private" means: do _not_ use these functions from outside!
// It is very likely that they will get change or removed.
* Validates a URL with global regex
* @param strin $url URL to validate
* @return bool true if URL is ok
// Only validate if regex is set
* Checks if a URL contains forbidden parts
* @param string $url URL to check
* @return bool True if URL is forbidden
while(!$ret && $i < $count)
* Inserts a URL to the database and returns the whole dataset
* @param string $url Url to shorten
* @return array Array with all available data inside, or empty one
// Clear unwanted chars from url
// We save timestamp and IP of submitter.
// MD5 is needed for verification and faster search on multiple insert
INET_ATON(\''. $this->_get_ip(). '\'));';
* Searches a Key in the database and returns the whole dataset
* @param string $key Key to search
* @return array Array with all available data inside, or empty one
$sql = 'SELECT *, INET_NTOA(ip) as ip_long FROM '.
* Searches a URL in the database and returns the whole dataset
* @param string $url Url to search
* @return array Array with all available data inside, or empty one
// Search for MD5 of URL because it is a CHAR Collumn with a INDEX on it.
$sql = 'SELECT *, INET_NTOA(ip) as ip_long FROM '.
* Converts a numeric id from decimal to alphanumeric.
* @param int|string$id Integer to convert
* @param bool $dir Direction to convert, default=true => normal
* @return string Converted id
* Returns the ip of current client
* Checks for clients behind proxys
* @return string IP of current client
$ip = $_SERVER['REMOTE_ADDR'];
if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if($_SERVER['HTTP_FORWARDED_FOR'])
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
if($_SERVER['HTTP_FORWARDED'])
$ip = $_SERVER['HTTP_FORWARDED'];
|