Mercurial > hg > egg-tcls
view urlredirect.php.txt @ 684:5163d582e396
quotedb: Change voting a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 28 Nov 2021 04:20:18 +0200 |
parents | 6c999a7ac6d9 |
children |
line wrap: on
line source
<? /* ========================================================== * URLLog PHP-script for redirecting ShortURLs * (C) Copyright 2006-2015 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. */ try { // You need to uncomment and configure one of the PDO object // initialization lines below, depending on what kind of SQL // database you are using. // If using SQLite3, set the filename / path there: //$db = new PDO("sqlite:/path/to/urllog.sqlite"); } catch (PDOException $e) { myerr("Could not connect to URL database: ".$e->getMessage()."."); } // ========================================================== // Helper functions function urlToID($url) { $idCodes = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; $idNumCodes = strlen($idCodes); $len = strlen($url); if ($len < 1) return -1; for ($id = 0, $i = 0; $i < $len; $i++) { $id *= $idNumCodes; $n = strpos($idCodes, $url[$i]); if ($n !== FALSE) $id += $n; else return -2; } return $id; } 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 $urlID = urlToID($urlStr); if ($urlID < 0) myerr("Invalid ID."); // Find the matching URL, if any 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."); ?>