Page 1 of 1

A SmallCode is giving me Trouble

Posted: Fri Mar 06, 2009 9:34 pm
by wee493
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)

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
        }
    }
}
?>
 

Re: A SmallCode is giving me Trouble

Posted: Fri Mar 06, 2009 10:57 pm
by susrisha

Code: Select all

 
else {
    // redirect
    if(is_dir($show)){
..............
 
There is no assignment to the variable $show in the else statement.
Hence it returns nothing.
I dont think further queries also will be loaded due to the same reason.

Re: A SmallCode is giving me Trouble

Posted: Fri Mar 06, 2009 11:25 pm
by wee493
susrisha wrote:

Code: Select all

 
else {
    // redirect
    if(is_dir($show)){
..............
 
There is no assignment to the variable $show in the else statement.
Hence it returns nothing.
I dont think further queries also will be loaded due to the same reason.
Thanks for the reply, but on the down side. Changing that did not fix the problem.

Re: A SmallCode is giving me Trouble

Posted: Sat Mar 07, 2009 11:48 am
by wee493
curadebt wrote:Is this coder worked, if not then i can post another one which is guaranteed to work.
Ya, It didn't Work. I'd like whatever you think will work