A SmallCode is giving me Trouble
Posted: Fri Mar 06, 2009 9:34 pm
I'm having a little trouble with getting this small bit of code right.
This site is a URL shortner (like tiny url). I'm doing an if to see if the user has entered text for a custom url, if not then the url equals some random characters.
The problem comes when a user does a custom url it is entered into the databes, but its not redirecting.
here is the code (The if for custom url begins at line 15)
This site is a URL shortner (like tiny url). I'm doing an if to see if the user has entered text for a custom url, if not then the url equals some random characters.
The problem comes when a user does a custom url it is entered into the databes, but its not redirecting.
here is the code (The if for custom url begins at line 15)
Code: Select all
if($_POST["shorten_submitted"] && $_POST["destination"]!=''){
$page->Set("destination",$_POST["destination"]);
// check up again DB for destination URL
if($db->query("SELECT * FROM url WHERE destination='".$_POST["destination"]."'")){
// url exist
$db->fetch();
$mini_url=$db->row["url"];
$page->Set("mini_url","$SITE_URL/$mini_url");
$page->Set("message","This URL has been shortened before, here it is again:");
} else {
// url does not exist, create new mini url
// generate random mini url and check against url database for existence
do {
if($_POST["custom"]) {
$rnd_url=$_POST["custom"];
$show=$_POST["custom"];
} else {
$rnd_url=chr(rand(97,122)).rand(0,9).chr(rand(65,90)).chr(rand(97,122)).rand(0,9);
}
} while ($db->query("SELECT id FROM url WHERE url='$newurl'"));
if(!mysql_query("INSERT INTO url (url, destination) VALUES ('".$_POST["custom"]."','".$_POST["destination"]."')")){
// insert failed
$page->Set("message","Database INSERT Failed. Please contact Webmaster.");
} else {
$page->Set("mini_url","$SITE_URL/$rnd_url");
$page->Set("message","The WiURL URL is:");
}
}
$page->Set("result",$page->CreatePage("templates/result.htm"));
} else {
// redirect
if(is_dir($show)){
header("location: $SITE_URL/$show");
} else {
// check up against DB for URL
// if adding .gif or .jpg, then still process redirect, but ignore .gif or .jpg
$ext=substr($show,-4,4);
if($ext=='.gif') $show=str_replace('.gif','',$show);
if($ext=='.jpg') $show=str_replace('.jpg','',$show);
if($db->query("SELECT destination FROM url WHERE url='$show'")){
// url exist
$db->fetch();
header('location: '.$db->row["destination"]);
} else {
// invalid short URL, dispaly homepage
}
}
}
?>