Mercurial > hg > egg-tcls
view urlredirect.php.txt @ 125:42cf6a768c46
tj: Add support for years in the output.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 27 May 2012 10:36:03 +0300 |
parents | 7ed449f9b848 |
children | 68c4b25c982c |
line wrap: on
line source
<? /* ========================================================== * URLLog PHP-script for redirecting ShortURLs * (C) Copyright 2006-2011 Tecnic Software productions (TNSP) * ========================================================== * How to set up short URL redirection: * * 1) Set up URLLog database (see urllog.tcl for more information) * * 2) In this example we assume that your short URL prefix is: * http://example.com/u/ * This setting is called urllog_shorturl_prefix in urllog.tcl * * 3) Place this PHP script under your www-server root. In this * example we will use "/var/www/urlredirect.php" that is * visible as http://example.com/urlredirect.php * * Edit script dbFilename setting below to point to correct * URLLog SQLite database file location. * * 4) Create or edit /var/www/.htaccess and add following: * RewriteEngine on * RewriteRule ^u/([^\?\&]+)$ /urlredirect.php?u=$1 * * 5) You may need to restart/reload your www-server. * * 6) Profit. */ // SQLite3 database file path and name $dbFilename = "/path/to/urllog.sqlite"; // ========================================================== // Helper functions $idStr = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; function myerr($str = "") { header("Status: 404 Not Found"); echo "404 Not Found - ".$str; exit; } // Check arguments if (isset($_GET["u"])) $urlStr = $_GET["u"]; else myerr("No ID given."); // Calculate urlID $urlLen = strlen($urlStr); if ($urlLen < 1) myerr("Invalid ID length."); for ($urlID = 0, $i = 0; $i < $urlLen; $i++) { $urlID *= strlen($idStr); $n = strpos($idStr, $urlStr[$i]); if ($n !== FALSE) { $urlID += $n; } else myerr("Invalid ID."); } // Find the URL try { $db = new PDO("sqlite:".$dbFilename); } catch (PDOException $e) { myerr("Could not connect to URL database: ".$e->getMessage()."."); } foreach ($db->query("SELECT url FROM urls WHERE id=".$urlID) as $row) { header("Location: ".$row["url"]); exit; } $db = null; // Output result myerr("No such ID in database."); ?>