exists($key) ) {
header("X-Cache: Hit");
echo $db->get($key);
ob_end_flush();
exit;
}
?>
Link Shorter
set($key, $html, 3600);
ob_end_flush();
} else {
if ( empty($_POST["url"]) || $_POST["url"] == "" ){
do_output("We need a link to be shortened.
", "400 Client Failed", false, "Missing URL
");
}
if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){
do_output("Only schemas like http:// or ftp:// are supported.
", "400 Client Failed", false, "This does not look like an url
");
}
$hash = md5($_POST["url"]);
if( ! empty($_POST["short"]) && $_POST["short"] != "" ) {
$short = $_POST["short"];
if ( $db->exists($short) == 1 && $_POST["url"] != $db->get($short) )
do_output("Someone else has already a registered entry under '".htmlentities($short)."'.
", "422 Unprocessable Entity", false, "Query string already exists.
");
} else {
if( ! $short = $db->get($hash) ){
$arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
do {
$short="";
for ($i=0;$i<5;$i++){
$r = mt_rand(0, count($arr)-1);
$short.=$arr[$r];
}
} while ( $db->exists($short) );
$db->set($hash, $short);
}
}
$db->set($short, $_POST["url"]);
do_output("Your short link for ".htmlentities($_POST["url"])." is
http://".$_SERVER["HTTP_HOST"]."/".$short."
", "200 OK", false, "Success
");
}