process(); /** --- -- Tabellenstruktur für Tabelle `urls` -- CREATE TABLE IF NOT EXISTS `urls` ( `code` varchar(15) COLLATE utf8_bin NOT NULL, `url` text COLLATE utf8_bin NOT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `counter` int(11) NOT NULL, PRIMARY KEY (`code`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Code zu URL Zuordnung'; ////////////////////// Code für die .htaccess Datei RewriteEngine On # SHORTENER: IHRE_DOMAIN.TLD/-CODE wird durch das Script short.php bearbeitet. RewriteRule ^-(.*)$ /short.php?code=$1 [R=301,L] ////////////////////// Weitere Information finden Sie unter http://webblog.servicehome.net/2010/06/02/url-shortener-aber-warum-unter-fremder-domane/ und http://webblog.servicehome.net/2010/06/03/url-shortener-bookmark-im-firefox/ */ class url_manager { private $db_connection; private $BASE_URL = "http://IHRE_DOMAIN.TLD"; // z.B. "http://servicehome.net/"; private $code_length = 6; // Länge des generierten Codes. /** * Create db-connection. if used in shop-environment use given db-connection! */ function __construct() { $this->db_connection = mysql_connect("localhost", "DB_BENUTZERNAME", "PASSWORT"); if ($this->db_connection) { mysql_select_db("DATENBANK_NAME", $this->db_connection); } } function __desctruct() { // NOTHING YET } function process() { // redirect? $code = isset($_GET['code']) ? $_GET['code'] : null; if (!is_null($code)) { $url = $this->getUrlByCode($code); if ($url != "") { $this->count($code); $this->redirect($url); } die(""); } // generate code? $url = isset($_GET['url']) ? $_GET['url'] : null; if (!is_null($url)) { $code = $this->storeUrlAndReturnCode($url); $this->displayShortenedUrl($code); } if (isset($_GET['stats'])) { $this->showStatistics(); } } private function showStatistics() { $query = 'SELECT code, url, counter, time FROM urls ORDER BY counter DESC;'; $rec = mysql_query($query); if ($rec) { while ($row = mysql_fetch_array($rec)) { $time = $row['time']; $code = $row['code']; $url = $row['url']; $counter = sprintf("%'.4d", $row['counter']); echo "{$counter} - {$time} / {$code}: {$url}\n
"; } } } private function count($code) { $query = "UPDATE urls SET counter=(counter+1) WHERE code='".$code."'"; mysql_query($query, $this->db_connection); } private function displayShortenedUrl($code) { echo $this->BASE_URL . "-" . $code; } private function storeUrlAndReturnCode($url) { $code = ""; if ($url != "") { $code = $this->getCodeByUrl($url); if ($code == "") { for ($i=0; $i < $this->code_length; $i++) { $c1 = rand(48,57); $c2 = rand(65,90); $c3 = rand(97,122); $selector = rand(1,3); switch ($selector) { case 1: $code .= chr($c1); break; case 2: $code .= chr($c2); break; case 3: $code .= chr($c3); break; } } // store new entry $this->createEntry($url, $code); } } return $code; } private function getCodeByUrl($url) { $code = $this->queryCodeByUrl($url); return $code; } private function createEntry($url, $code) { $url = urldecode($url); $query = "INSERT INTO urls (url,code) VALUES ('".$url."', '".$code."')"; mysql_query($query, $this->db_connection); $error_number = mysql_errno($this->db_connection); if ($error_number != 0) echo "Fehler beim speichern! (ERR:".$error_number.") url:".$url." code:".$code; } private function queryCodeByUrl($url) { $code = ""; $query = "SELECT code FROM urls WHERE url='".$url."'"; $rec = mysql_query($query, $this->db_connection); if ($rec) { $data = mysql_fetch_assoc($rec); $code = $data['code']; } return $code; } private function queryUrlByCode($code) { $url = ""; $query = "SELECT url FROM urls WHERE code = '".$code."'"; $rec = mysql_query($query, $this->db_connection); if ($rec) { $data = mysql_fetch_assoc($rec); $url = $data['url']; } return $url; } function getUrlByCode($code) { $url = $this->queryUrlByCode($code); return $url; } private function redirect($url) { header('Location: '.$url); } }