PHP ip记录器错误


PHP ip logger error

我有这个代码:

   <?php
    echo $_POST['id-input'];
    $time = time();
    $ip=$_SERVER['REMOTE_ADDR'];
    $userid_c = $_POST['id-input'];
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
     mysql_select_db("kritya20_data", $con);
     $sql = "SELECT * FROM users_online WHERE user_id='$userid_c'";
     $query = mysql_query($sql);
     $rows_only_c = mysql_num_rows($query);
     if($rows_only_c == 0)
     {
        $sql1= "INSERT INTO users_online (user_id , online_since , lastup , lobbieid)
        VALUES ('$userid_c' , '$time' , '$time' , '1')";
        $query1 = mysql_query($sql1);
     }
     elseif($rows_only_c==1)
     {
        $sql2 = "UPDATE users_online
        SET lastup='$time'
        WHERE user_id='$userid_c' ";
        $query2 = mysql_query($sql2);
     }
     $sql3 = "SELECT * FROM iptable WHERE user_id='$userid_c'";
     $query3 = mysql_query($sql3);
     $row_ip = mysql_num_rows($query3);
     if($row_ip==0)
     {
          $sql4 = "INSERT INTO iptable (user_id , ip , added)
          VALUES ('$userid_c' , '$ip' , '$time')";
          $query4= mysql_query($sql4);
    }
    elseif($row_ip>0)
    {
        $mt=0;
        $mf=0;
        $sql4 = "SELECT * FROM iptable WHERE user_id='$userid_c'";
        $query4 = mysql_query($sql4);
        while($row=$query4)
        {
          if($ip==$row[2])
          {
              $mt++;
          }
          else
          {
              $mf++;
          }
        }
        if($mf!=0)
        {
            $sql5 = "INSERT INTO iptable (user_id , ip , added)
            VALUES ('$userid_c' , '$ip' , '$time')";
            $query5= mysql_query($sql5);
        }
    }
?>

我哪里出错了O因为要么它只是第一次添加,要么每当ip更改时都不会添加。

我修改了下面的代码,以改进布局、描述性变量名称、添加调试、建议的逻辑改进,这些改进简化了代码并修复了您的错误。

请阅读并尝试在未来的代码中使用其中的一些内容,它将极大地帮助您调试或编写人们能够理解的代码。我不想听起来像个屈尊俯就的混蛋,我们都是从某个地方开始的,我仍然会犯愚蠢的错误!

如果我做了什么你不明白(或者代码,或者我为什么这么做),请问。

<?php
    $debug_on = true;
    #Variable section
    $time = time();
    $ip=$_SERVER['REMOTE_ADDR'];
    $userid_c = $_POST['id-input'];
    if($debug_on) {
        echo "<p>time: '$time'</p>";
        echo "<p>ip: '$ip'</p>";
        echo "<p>userid_c: '$userid_c'</p>";
    }
    #Connect to database
    $con = mysql_connect("localhost","username","password");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("kritya20_data", $con) or die('Could not select database');
    #Insert or update users_online
    $user_exits_sql = "SELECT * FROM users_online WHERE user_id='$userid_c'";
    $user_exits_query = mysql_query($user_exits_sql);
    $user_exists = mysql_num_rows($user_exits_query);
    if($user_exists == 0) {
        $insert_user_sql = "INSERT INTO users_online (user_id , online_since , lastup , lobbieid) VALUES ('$userid_c' , '$time' , '$time' , '1')";
        if($debug_on) {
            echo "<p>No record found in `users_online` - Inserting new record</p>";
            echo "<p>$insert_user_sql</p>";
        }
        mysql_query($insert_user_sql);
    }
    else { # no need for 'elseif($rows_only_c==1)' as this is the only other option
        $update_user_sql = "UPDATE users_online
                            SET lastup='$time'
                            WHERE user_id='$userid_c' ";
        if($debug_on) {
            echo "<p>Record found in `users_online` - Updating existing record</p>";
            echo "<p>$update_user_sql</p>";
        }
        mysql_query($update_user_sql);
    }
    #Insert or update iptable
    $user_ip_exists_sql = "SELECT * FROM iptable WHERE user_id='$userid_c'";
    $user_ip_exists_query = mysql_query($user_ip_exists_sql);
    $user_ip_exists = mysql_num_rows($user_ip_exists_query);
    if($user_ip_exists==0) {
        $user_ip_insert_sql = "INSERT INTO iptable (user_id , ip , added)
                               VALUES ('$userid_c' , '$ip' , '$time')";
        if($debug_on) {
            echo "<p>No record found in `iptable` - Inserting new record</p>";
            echo "<p>$user_ip_insert_sql</p>";
        }
        mysql_query($user_ip_insert_sql);
    }
    else { # 'elseif($row_ip>0)'
        $mt = 0;
        $mf = 0;
        $iptable_sql = "SELECT * FROM iptable WHERE user_id='$userid_c'";
        $iptable_query = mysql_query($iptable_sql);
        #The next line is your bug,
        #should read while($row = $iptable_query->mysql_fetch_array())
        while($row = $iptable_query) {
          if($ip==$row[2]) {
              $mt++;
          }
          else {
              $mf++;
          }
        }
        # For bonus points you could use $iptable_query->mysql_fetch_row(), then $row->ip rather than $row[2]. 
        # This makes it obvious what variable you are trying to access.
        if($mf != 0) {
            $iptable_insert_sql = "INSERT INTO iptable (user_id , ip , added)
                                   VALUES ('$userid_c' , '$ip' , '$time')";
            if($debug_on) {
                echo "<p>No record found in `iptable` for user with this ip address - Inserting new record</p>";
                echo "<p>$iptable_insert_sql</p>";
            }
            mysql_query($iptable_insert_sql);
        }
        # This whole section seems to be:
        # If user doesnt exist, add them,
        # If user does exist, but not for this ip, add them
        # If this is right this could be made much simpler by changing $user_ip_exists_sql to be where user_id='$userid_c' and ip='$ip'
        # then you could remove all the $mt, $mf stuff
    }
?>

代码可能会有一些改进。但是,尝试将while($row=$query4)更改为while($row=mysql_fetch_array($query4))

同样,由于您要运行$sql3 = "SELECT * FROM iptable WHERE user_id='$userid_c'";两次,为什么不直接使用第一个查询的结果集,而不是向数据库发送另一个查询呢。