PHP ip logger错误

时间:2011-06-05 08:37:45

标签: php

我有这段代码:

   <?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改变它就不会。

2 个答案:

答案 0 :(得分:1)

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

请阅读并尝试在将来的代码中使用其中的一些内容,它将极大地帮助您进行调试或只编写人们可以理解的代码。我不是听起来像一个居高临下的屁股,我们都开始在某个地方,我仍然犯了愚蠢的错误!

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

<?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
    }
?>

答案 1 :(得分:0)

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

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