Jump to content


Photo

IF / ELSE not working - not sure why


  • Please log in to reply
15 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 04:29 PM

Summary: This landing page is supposed to check the visitor's IP address against a data table to see if it's been 'banned'. If so, displays one message that they are banned. If not, performs another function and displays entirely different message and logs the visitor's IP address into the database.

The first section of this code is supposed to check against the IP's in the 'banned_ip' table and return a true or false then proceed accordingly.

//check for the possibility if it is banned before we write it to the database
function isbanned($ip) {
  $sql = "SELECT banned_ip FROM banned_ip WHERE ip = '$ip'";
  if ($result = mysql_query($sql)) {
    return true;
  }
  return false;
}

if (isbanned($_SERVER['REMOTE_ADDR'])) {
  echo "<table align='center' width='500' bgcolor='C0C0C0'><tr><td><center><font face='Verdana'><h3>Your IP address has been banned from accessing this site.</h3><font face='Verdana' size='2'> If you feel this is in error you can contact the site administrator <a mailto='$site_email'>via email</a><br></td></tr></table>";
} else {
// go about whatever it is you want to do.

// Insert a row of information into the table "iplog"
mysql_query("INSERT INTO iplog 
(ip, date, directory) VALUES('$ip', '$v_date','$dir') ") 
or die(mysql_error());


print "<table width='500' align='center' border='1'><tr><td>";
print "<table width='100%'><tr><td bgcolor='#C8D4DF'>";
echo "<center><font face='Verdana'><h3>Unauthorized Access Warning Message</h3></font>";
print "</td></tr>";
print "<tr><td bgcolor='#FFF0C0'>";
print "<center><h3>You should not be here</h4><p>";
print "</td></tr>";
print "<tr><td bgcolor='#FFF0C0'>";
echo "<center><font size='2' face='Verdana'>Your IP address is ". $ip . " and has been logged</font><p>";

echo "$v_date<p>";

echo "<center><font size='1' face='Verdana'><strong>You will be redirected to an authorized page.</strong></font>";
print "</td></tr></table></td></tr></table>";

}

What's happening is it appears to be skipping the validation and displays the second set of HTML about the logging of their IP address. I have banned my own IP address and then returned to the page that should be telling me i've been banned but it's not working. Ideas?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 04:33 PM

Try this:
function isbanned($ip) {
  $sql = "SELECT banned_ip FROM banned_ip WHERE ip = '$ip'";
  $result = mysql_query($sql);
  if (mysql_num_rows($result) == 1) {
    return true;
  } else {
    return false;
  }
}

if (isbanned($_SERVER['REMOTE_ADDR']) === true) {


#3 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 04:42 PM

Hey, thanks for the ultra-fast response!

Ok, I replaced the current function with your changes. It still displays the 'else' information instead of the 'You're banned...' as it should and produces this error (which, i'm assuming is why it's not displaying the 'You're banned... ' text):

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xxxxxxx/public_html/iptest2/index.php on line 24

This is line 24:

if (mysql_num_rows($result) == 1) {


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 04:46 PM

Change this:
$result = mysql_query($sql);
to this:
$result = mysql_query($sql) or die("Unable to perform query:<i>{$sql}</i><br />" . mysql_error());
It apears your query is failing. If it is failing it should give an error.

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 July 2006 - 04:49 PM

Silly question but... you do have a table called banned_ip with a field called ip? I was just using them as an example when I posted the original function.

#6 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 05:11 PM

thorpe, yes the table/fields are right and they are actually named 'banned_ip' for each and I did change them from your original post. Turns out the problem was something in the copy/paste of the code from this post to my file. Not sure why or how it did it but it left off the ; on the first 'if' statement which caused it to break. I fixed that and then got the next error about line 24 which didn't make sense either. So, I copied the code again but this time stuck it into a text editor to strip out the extra characters, pasted it in, changed the $result line to the one wildteen88 recommended and now it's working.

I do have one more question. I want to run this javascript redirect code IF the results return as false. Right now I have it placed outside the PHP brackets so it redirects no matter what. What I want to happen is that IF they are indeed on the banned list that the 'You're banned...' message displays and stays there. Only IF they are not banned should it redirect. Here's the code:

<script language=javascript> 

// Adjust the time and URL for the redirect. 1000 equals 1 second. Currently set at 6 secs.
window.setTimeout("location='http://www.yoursitename.com/index.php'",6000);

</script>

Is there a way to execute this inside the PHP tags and IF they return false only? Or, should I use a PHP 'header' method to do it instead?

#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 July 2006 - 05:15 PM

Just use a call to header().

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 05:17 PM

Id use a header redirect.
header("Refresh: 6; URL=http://www.yoursitename.com/index.php");
before this line:
echo "<table align='center' width='500' bgcolor='C0C0C0'><tr><td><center><font face='Verdana'><h3>Your IP address has been banned from accessing this site.</h3><font face='Verdana' size='2'> If you feel this is in error you can contact the site administrator <a mailto='$site_email'>via email</a><br></td></tr></table>";

Just make sure you dont have any output before the use of header, otheriwse you'll have to either use ob_start(); and ob_end_flush(); for output buffering, or use javascript for the redirect.

#9 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 05:33 PM

I got this error when I inserted the header code where you specified:

Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxxx/public_html/iptest2/config.php:15) in /home/xxxxxx/public_html/iptest2/index.php on line 32


In my 'logical' mind I thought that the javascript code could be executed if placed in the 'else' section which would make sense since I only want them to be redirected IF they are not banned. The idea is if they reach an unauthorized area then the script would redirect them to an authorized area. If they are banned from the site it should just display the banned message and not redirect. Or, I could be a real prick and have it redirect them to some porn site or similar. But, then again, that may not be punishment ;)

So, in my noobiest way, I attempted to place the javascript code inside the else statment, got an error about unexpected '<', placed some quotes around it, got more errors, tried an echo command, got an error... rinse, repeat 45 times. So...

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 05:37 PM

If you are going to use your javascript you'll have to echo it in order for the browser to parse the HTML. However the reason why your header redirect isn't working is bacause on line 15 in config.php yuou have output. What on line 15 or around line 15 in config.php

#11 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 05:52 PM

The config file contains strictly variables. Line 15 is actually a comment //

Here's the lineup:

<?php 
//comment
//comment 
//another comment

// MySQL Database information - edit these variables
$dbname = 'database_name';
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'dbpassword';
// Admin password
$admin_password = "eatpotatoes";
// Site URL
$siteurl = 'http://www.xxxxxxx.com/iptest/csv/iplog.zip';
// Site email
$site_email = 'yourname@yourdomain.com';
 ?>

In the main script there's two include files at the top before your header statement. One is the include 'config.php' and the other is include 'header.php' which contains basically a title tag and image reference.

#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 06:01 PM

Thats might be the problem then. Its to do with the the header.php file as that is outputting text/html to browser, which will cause the header function to not work. You'll have to either use output buffering or just echo out your javascript inplace of the header function.

#13 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 06:56 PM

Ok, I commented out the include 'header.php' and it still shows that same error. So, it's not something in the header file. It keeps pointing to the last line of the config.php file as the culprit but that's just the closing PHP tag.

I tried a few ways to get the javascript to parse on an echo call but couldn't seem to get it to work. Is there a special format I have to put that in?

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 07:09 PM

Is there any whitespace after the closing PHP tag? for example a new line a space etc, in config.php

#15 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 07:17 PM

I checked the config.php and there was a blank white space after the closing tag. I deleted that, reuploaded and then the error switched to pointing to the header.php as the problem. Since the header.php file only contains one line calling an image I commented out the include statement and wrote an echo statement to call the image and now no errors show up.

However, it's still redirecting if the IP is banned instead of just displaying the message.

#16 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 07:33 PM

I moved the header() statement to the 'else' section and now it works fine. The original point of insertion you specified was in the 'if' section which would then redirect the page if the person was indeed banned. Now it just displays the message as it should.

Thanks for all the help! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users