I haven't bothered looking through all that code but the idea is quite simple. Firstly, you need a form which will take a users long url, this part is simple. Once you have this long url, you need to store along side it in the database a unique random string, (again a simple task).
From there all you need is to create a script which excepts the unique string through $_GET and looks that up in the database to find the actual long url. Something like....
<?php
if (isset($_GET['key'])) {
$key = mysql_real_escape_string($_GET['key']);
$sql = "SELECT url FROM urls WHERE key = '$key' LIMIT 1";
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
$row = mysql_fetch_assoc($result);
header ("Location: {$row['url']}");
} else {
echo "Sorry, that key does not match any url in our database";
}
}
}
You then just create a simple mod_rewrite rule to rewrite the short url to a query string.
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /redirector.php?url=$1 [L]
This would then allow users to type address such as http://short.com/he8dne to be redirected to whatever address is stored with the key of he8dne.