Hi. I am having trouble with this script:

<?php
$host="mysql.host.com"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="DB_NAME"; // Database name 
$tbl_name="table"; // Table name



$myusername = $_SESSION['myusername']; // Session on login page, in a variable.

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Select everything that we need.
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and banned='1'"; // Possible problem...I think....
$query = mysql_query($sql);



$row = mysql_fetch_array($query);

if($banned == 1) { // If the person on the table has the "1" value in banned, ban them..

header('Location: http://www.mywebsite.com/banned/banneduser.php'); // Ban page

} else {

// Their not banned... Let them in ..... Don't do anything...

}
?>

This code works, it's just not working properly. This code is supposed to redirect the user with the "1" value in the column "banned". Basicly, this script is supposed to "crawl" on the table and search for the user in the table with the "1" value in "banned", while the user is online, and redirect them to the "banned" page if the "banned" value is "1". The script currently does not redirect how I want it to. Instead, it lets the people that are banned online. I don't know why. I tried jumping through all the hoops, and never got it working. I would be glad if someone fixed it.

$banned has no value, I think you mean to use $row['banned']

Change this:

$query = mysql_query($sql);

with:

$query = mysql_query($sql) or die(mysql_error());

and check what you get. Also the session of the banned user should be destroyed.

Now it's only going to the ban page. No matter if the user is banned or not. I tried adding:

$query = mysql_query($sql) or die(mysql_error());

and fixing the if line to:

if($row['banned'] == 1) {

header('Location: http://www.aphpsite.comuv.com/ban/banpage.php');

} else {

// Let them in .....

}

All the MySQL errors are gone, it's just redirecting to the ban page no matter if your banned. It's like it skips the else statement.

Got it working. It was the else statement. I had to take it out. Thanks for your guys help.

Member Avatar for diafol

Dunno if it's still relevant, but I always put an exit; after any header redirect.

I know it's really pathetic, but I just had a dream about that. I had to sift through all my code putting an exit(); after all my header redirects. I haven't done it for real, but that was last night's nightmare, I kid you not. I need to get out more.

Member Avatar for diafol

Whoah nightmare - wide-eyed and preg-less. Could it be a message from the subconscious, telling you to find the exit so that you CAN go out more? Try it - I did on Friday night. Still suffering - from cringing - I told all my mates I loved them. Come to think of it Dani, I think I told you I loved you too. :)

Freedom may be over-rated after all...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.