HOW TO?: Look at all rows and find same IP's & echo.

Discussion in 'PHP' started by donthate, May 26, 2007.

  1. #1
    Hello,

    Can someone give me a simple php code where it checks my database row i.e called ip and every users ip is recorded, and if there are any IPs that are the same it will echo/print the IP that is the same.

    Shouldnt be hard, thanks!
     
    donthate, May 26, 2007 IP
  2. tinkerbox

    tinkerbox Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is this what you want? :)
    
    $visitor_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      $visitor_ip = $_SERVER['REMOTE_ADDR'];
      if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
          $visitor_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
          $visitor_ip = $_SERVER['REMOTE_ADDR'];
      }   
    
      $query = "SELECT ip FROM visitor WHERE ip = '$visitor_ip'";
      $result = mysql_query($query) or die(mysql_error());
      if (mysql_num_rows($result) > 0) {
        echo "Ip matched in database : $visitor_ip";
      } else {
        echo "This IP is new : $visitor_ip";
      }
    
    PHP:
    Or did you meant duplicate ips:
    
    $query = "SELECT ip FROM visitor GROUP BY ip";
      $result = mysql_query($query) or die(mysql_error());
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
          echo "Duplicate IP in database : ".$row['ip']."<br />";
      }
    
    PHP:
     
    tinkerbox, May 27, 2007 IP