A SmallCode is giving me Trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wee493
Forum Newbie
Posts: 22
Joined: Sat Sep 06, 2008 3:05 pm

A SmallCode is giving me Trouble

Post 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
        }
    }
}
?>
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: A SmallCode is giving me Trouble

Post 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.
wee493
Forum Newbie
Posts: 22
Joined: Sat Sep 06, 2008 3:05 pm

Re: A SmallCode is giving me Trouble

Post 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.
wee493
Forum Newbie
Posts: 22
Joined: Sat Sep 06, 2008 3:05 pm

Re: A SmallCode is giving me Trouble

Post 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
Post Reply