Jump to content

IF / ELSE not working - not sure why


simcoweb

Recommended Posts

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.

[code]//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>";

}[/code]

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?
Link to comment
Share on other sites

Try this:
[code]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) {[/code]
Link to comment
Share on other sites

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:

[code]if (mysql_num_rows($result) == 1) {[/code]
Link to comment
Share on other sites

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

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:

[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>[/code]

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?
Link to comment
Share on other sites

Id use a header redirect.
[code]header("Refresh: 6; URL=http://www.yoursitename.com/index.php");[/code]
before this line:
[code]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>";[/code]

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.
Link to comment
Share on other sites

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

[quote]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[/quote]

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...
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Here's the lineup:

[code]<?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';
?> [/code]

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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! :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.