function show_form()
{
?>
tinyurl.pruts.nl
|
}
function add_url($db, $url, $private)
{
global $_SERVER;
$url = htmlspecialchars($url);
# Check if the URL is valid
if(! preg_match("/^\S+\:\/\/.+/i", $url)) {
print "$url is not a valid url";
return;
}
# See if the url is already in the database
$result = mysqli_query($db, "select id from tinyurl where url='$url'");
$row = mysqli_fetch_array($result);
$id = $row['id'];
# If not found, add it
if(! isset($id)) {
$submit_ip = $_SERVER['REMOTE_ADDR'];
mysqli_query($db, "insert into tinyurl (url, time, submit_ip, clicks, private) values ('$url', now(), '$submit_ip', 0, '$private')");
$id = mysqli_insert_id();
}
#
# Create url
#
$id = base_convert($id, 10, 36);
$tinyurl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$tinyurl = preg_replace("/\?.+/", "", $tinyurl);
$tinyurl .= "$id";
?>
}
function load_url($db, $tiny)
{
if($tiny == "latest") {
$result = mysqli_query($db, "select max(id) as id from tinyurl");
$r = mysqli_fetch_assoc($result);
$id = $r['id'];
} else {
$id = base_convert($tiny, 36, 10);
}
$result = mysqli_query($db, "select clicks, url from tinyurl where id=$id");
$r = mysqli_fetch_assoc($result);
$url = $r['url'];
$clicks = $r['clicks'];
if( strlen($url)) {
$clicks ++;
mysqli_query($db, "update tinyurl set clicks=$clicks where id=$id");
print mysqli_error();
header("Location: $url");
return;
} else {
?>
Sorry, url not found.
|
return;
}
}
$db = mysqli_connect("localhost", "tinyurl", "tinyurl");
mysqli_select_db($db, "pruts");
if (mysqli_connect_errno())
{
print(mysqli_connect_error());
}
$url = $_REQUEST['url'];
$private = $_REQUEST['private'];
$qs = $_SERVER['QUERY_STRING'];
if(strlen($url) > 0) {
add_url($db, $url, $private);
exit;
} elseif(strlen($qs) > 0) {
load_url($db, $qs);
exit;
} else {
?>
print "";
show_form();
exit;
}