query to compare two IP addresses and return username

Hi,

Im using the following code to bring up a list of IP addresses that have had a fialed login on my system:
$host = "localhost";
$user = "substan";
$pass = "XXXXXXX";
$dbname = "substan_template";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error());
mysql_select_db($dbname) or die (mysql_errno().": ".mysql_error()); ;

  $query= "SELECT * FROM log_tables";
  $result = mysql_query($query) or die ("Sql error : " . mysql_error());
 // echo mysql_num_rows($result) . " number of records found<br>";
  while($row = mysql_fetch_array($result)) {
 
   echo "<br>" . "<b>log #</b>: " . $row['id'] . "<br><br><b>IP Address: </b>" . $row['ip_column'] . "<br><b>Date/Time: </b>" . $row['logged_datetime'] . "<br>"; //receipt display
   
;
     
  }


 

?>

what i want to do is add an additional field which compares each IP generated to those of people who are members of the website. users Ip addresses are taken into a 'users' table on signup in field called 'signup_ip'.

So basiaclly i need a field that checks to see if each IP matches any other in the userss table and if it does, i would like to print the username next to the current code i have above.

thanks a lot!
LVL 2
clonmelogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi clonmelog,

$sql = "SELECT * FROM users WHERE signup_ip = $ipaddress";
$result = mysql_query($sql, $connection);
if (mysql_num_rows($result) > 0)
{
  // Found
}
else
  print "IP address $ipaddress did not match any ip address in the user tabe";

Of course you would need to define $ipaddress.

Joe P
0
TomeeboyCommented:
Hi clonmelog,

If you wanted it to display a list of matching user accounts for that IP address for each log entry, you could add another query in your while loop:

while($row = mysql_fetch_array($result)) {
 
    echo "<br>" . "<b>log #</b>: " . $row['id'] . "<br><br><b>IP Address: </b>" . $row['ip_column'] . "<br><b>Date/Time: </b>" . $row['logged_datetime'] . "<br><b>Matching User Accounts: </b>"; //receipt display
   
    $sql2 = "SELECT username FROM users WHERE ip_column = '" . $row['ip_column'] . "'";
    $result2 = mysql_query($sql2) or die(mysql_error());
 
    whille($row2 = mysql_fetch_assoc($result2)) {
        $matches[] = $row2['username'];
    }

    if (!empty(count($matches))) {
        echo implode(", ", $matches);
    }
    echo "<br>";    
}

Hope this helps!  ;)
0
clonmelogAuthor Commented:
Hmmm yes i guessed that would be something like the solution but my problem is that the vlaue of the IP us inside the loop in the vlaue  . $row['ip_column'] . and i want the data to be displayed in such a way that it gives a result for weather each Ip matched after each IP address is printed.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

TomeeboyCommented:
For better display, modify this part:

    if (!empty(count($matches))) {
        echo implode(", ", $matches);
    }


To this:

    if (!empty(count($matches))) {
        echo implode(", ", $matches);
    } else {
        echo "No matches";
    }
0
clonmelogAuthor Commented:
ive tried to fix this but i keep getting the following error:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /home/substan/public_html/admin_area/security_logs.php on line 131


this would be the lines:

   if (!empty(count($matches))) {
        echo implode(", ", $matches);
    } else {
        echo "No matches";
    }

ive made a few changes to the code to suit my own db table names etc... so the code reads..

<?
$host = "localhost";
$user = "substan";
$pass = "xxx";
$dbname = "substan_template";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error());
mysql_select_db($dbname) or die (mysql_errno().": ".mysql_error()); ;

  $query= "SELECT * FROM log_tables";
  $result = mysql_query($query) or die ("Sql error : " . mysql_error());
 // echo mysql_num_rows($result) . " number of records found<br>";
  while($row = mysql_fetch_array($result)) {
 
    echo "<br>" . "<b>log #</b>: " . $row['id'] . "<br><br><b>IP Address: </b>" . $row['ip_column'] . "<br><b>Date/Time: </b>" . $row['logged_datetime'] . "<br><b>Matching User Accounts: </b>"; //receipt display
   
    $sql2 = "SELECT uname FROM users WHERE signup_ip = '" . $row['ip_column'] . "'";
    $result2 = mysql_query($sql2) or die(mysql_error());
 
    while($row2 = mysql_fetch_assoc($result2)) {
        $matches[] = $row2['uname'];
    }

     if (!empty(count($matches))) {
        echo implode(", ", $matches);
    } else {
        echo "No matches";
    }

 

?>  
0
BogoJokerCommented:
Again:  $sql2 = "SELECT uname FROM users WHERE signup_ip = '" . $row['ip_column'] . "'";
Could be:  $sql2 = "SELECT uname FROM users WHERE signup_ip = '$row[ip_column]'";
But I guess it is personal preference.
0
TomeeboyCommented:
Sorry, you may not be able to have another function inside of empty().. Try this method instead:

if (isset($matches)) {
        echo implode(", ", $matches);
    } else {
        echo "No matches";
    }
0
clonmelogAuthor Commented:
tomeeboy, that got rid of my errors but the usernames diplayed are weird:

log #: 1

IP Address: 83.147.170.52
Date/Time: 2006-04-04 18:19:58
Matching User Accounts: fff
log #: 2

IP Address: 83.147.170.52
Date/Time: 2006-04-05 12:58:42
Matching User Accounts: fff, fff
log #: 3

IP Address: 83.147.170.52
Date/Time: 2006-04-05 12:59:32
Matching User Accounts: fff, fff, fff
log #: 4

IP Address: 83.147.170.52
Date/Time: 2006-04-05 13:00:15
Matching User Accounts: fff, fff, fff, fff
log #: 5

IP Address: 193.1.184.254
Date/Time: 2006-04-05 13:01:17
Matching User Accounts: fff, fff, fff, fff
log #: 6

IP Address: 83.167.254.16
Date/Time: 2006-04-05 16:58:26
Matching User Accounts: fff, fff, fff, fff
log #: 7

IP Address: 83.167.254.16
Date/Time: 2006-04-05 16:59:24
Matching User Accounts: fff, fff, fff, fff
log #: 8

IP Address: 83.147.170.52
Date/Time: 2006-04-05 16:59:39
Matching User Accounts: fff, fff, fff, fff, fff
log #: 9

IP Address: 83.147.170.52
Date/Time: 2006-04-05 17:16:08
Matching User Accounts: fff, fff, fff, fff, fff, fff
log #: 10

IP Address: 83.147.170.52
Date/Time: 2006-04-13 12:45:38
Matching User Accounts: fff, fff, fff, fff, fff, fff, fff

There is one user called "fff" in my db but obviously he couldnt have the different signup IP addesses as above.
0
TomeeboyCommented:
Sorry.. oversight on my part, since it's re-using the same array to hold the usernames.  After this part:

    if (!empty(count($matches))) {
        echo implode(", ", $matches);
    } else {
        echo "No matches";
    }

Add:

    unset($matches);

That should fix it.
0
clonmelogAuthor Commented:
add this to which line?>
0
TomeeboyCommented:
Right after the if statement I just posted (also, just realized I posted the bad code again in my last message, hopefully you didn't change that back.. heh):

    if (isset($matches)) {
        echo implode(", ", $matches);
    } else {
        echo "No matches";
    }
    unset($matches);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.