view urlredirect.php.txt @ 73:646b2fd67312

urllog: Improve documentation of different settings.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 11 Sep 2011 18:03:01 +0300
parents eb17a6bfd443
children 7ed449f9b848
line wrap: on
line source

<?
// ==========================================================
// URLLog PHP-script for redirecting ShortURLs
// (C) Copyright 2006-2011 Tecnic Software productions (TNSP)
// ==========================================================
// SQLite3 database file path and name
$dbFilename = "../db/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.");
?>