Redirect with mysql query

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
kobitriki
Forum Newbie
Posts: 1
Joined: Sun Nov 16, 2008 3:36 pm

Redirect with mysql query

Post by kobitriki »

Hi!
i want to create short url service website. (4url dot net)
currently unindexed IDs redirect to the main page except if the ID contains numbers only then its redirect to blank page.

the PHP code is(this is independent file which receive the ID from typed url):

Code: Select all

<?
 
 
ob_start();
include("config.php"); 
if(isset($_GET['id'])) {
    $gid = mysql_real_escape_string($_GET['id']);
    $qs = mysql_query("SELECT url FROM $table WHERE tag = '$gid';") or die('MySQL error: '.mysql_error());
    if (mysql_num_rows($qs) == 0) {
        $qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;") or die(header("Location: /"));
        mysql_query("UPDATE url SET count=(count + 1) WHERE `id` = $gid;") or die('MySQL error: '.mysql_error());
    } else
        mysql_query("UPDATE url SET count=(count + 1) WHERE `tag` = '$gid';") or die('MySQL error: '.mysql_error());
    while($data = mysql_fetch_array($qs)) {
        if ($frame == 1) {
            include("frame.php");
            echo "<iframe name=\"pagetext\" height=\"100%\" frameborder=\"no\" width=\"100%\" src=\"$data[0]\"></iframe>";
        } else if ($frame == 0) {
 
            if ( !preg_match( "/^http/", $data[0] ) ) {
                header("Location: http://$data[0]");
            } else {
                echo header("Location: $data[0]");
            }
        }
    }
}
 
ob_flush();
?>
hope some one can help on this,

Thanks
Kobi
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Redirect with mysql query

Post by Eran »

You can't put array indexes inside double quotes. Use concatenation instead:

Code: Select all

 
if ( !preg_match( "/^http/", $data[0] ) ) {
        header("Location: http://" . $data[0]);
} else {
       header("Location: " . $data[0]);
}
 
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Redirect with mysql query

Post by Syntac »

pytrin wrote:You can't put array indexes inside double quotes.
Yes you can. This may not be the case with string indexes, however.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Redirect with mysql query

Post by Eran »

This may not be the case with string indexes, however.
Yes, you are correct, my bad.

Try and dump the contents $data to see what it contains.
Post Reply