根据用户的IP地址 - mysql数据库与在线用户合作

时间:2015-06-15 01:09:39

标签: php mysql performance

现在我有一个框(在页脚中),我根据三种类型的网址,用户类型和他的IP地址显示在线用户。

我正在使用单个查询来删除,插入和显示在线用户,但我确实需要更多性能,因为我不确定这是否是最快的方法。也许如果有人在我的代码中看到任何不好的做法。

我们走了:

网址位置类型:

  1. 空网址(www.example.com)
  2. Url,例如www.example.com/forum/123 /
  3. Url,例如www.example.com/topic/123 /
  4. 用户类型:

    1. 访客
    2. 注册用户
    3. 这是我获取用户IP地址的方式:

      $ip_lookup = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR');
      foreach($ip_lookup as $server_param)
      {
          if(isset($_SERVER[$server_param]) && filter_var($_SERVER[$server_param], FILTER_VALIDATE_IP))
          {
              if(filter_var($_SERVER[$server_param], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
              {
                  define('dpd_ip_type','v6');
              }
              else
              {
                  define('dpd_ip_type','v4');
              }
              define('dpd_ip',inet_pton($_SERVER[$server_param]));
              break;
          }
      }
      if(!defined('dpd_ip') || dpd_ip === false) { error('error_ip'); exit(); }
      

      这里我有一个数组(其中一个基于url类型或用户类型):

      $online['dpd_ip'] = dpd_ip; /* The ip (ipv4 or ipv6) - this was converted as binary width inet_pton */
      $online['dpd_ip_type'] = dpd_ip_type; /* The ip type (ipv4 or ipv6) */
      $online['dpd_time'] = dpd_time; /* The current time in Unix Timestamp format */
      $online['url'] = ''; /* The url location - Empty by default */
      $online['user_id'] = ''; /* The user id - Empty by default */
      $online['user_name'] = ''; /* The user name - Empty by default */
      $online['get_online_users_where'] = ''; /* The where clause used for for output records - empty by default */
      $online['online_clear_where'] = ''; /* The where clause used for deleting records - empty by default */
      
      if(is_user())
      {
          $online['user_id'] = user_id; /* Set user_id because we talk about an registered user */
          $online['user_name'] = user_name; /* Set user_name because we talk about an registered user */
          $online['online_clear_where'] = " OR online_user_id = ".user_id; /* Set where clause for deleting this user before insert him again */
      }
      
      if(defined('url_1') && defined('url_2') && (url_1 == 'topic' || url_1 == 'forum'))
      {
          $online['url'] = url_1.'/'.url_2; /* The url is a section like forum/123/ or topic/123 */
          $online['get_online_users_where'] = " WHERE online_url = '".url_1.'/'.url_2."'"; /* Output the user that are accessed this url location */
      }
      

      在这里,我开始使用数据库:

      pdo('begin');
      
      /* Delete records from database if:
          - The records are bigger than 15 minutes ( dpd_footer_online_time is set to 900 seconds )
          - This is the actual user 
      */
      $online_clear = pdo("DELETE FROM online WHERE online_timestamp < ".(dpd_time - dpd_footer_online_time)." OR online_ip_".$online['dpd_ip_type']." = '".$online['dpd_ip']."'" . $online['online_clear_where']);
      
      /* Insert the user into database */
      $online = pdo("
          INSERT INTO
              online
              (
                  online_user_id,
                  online_user_name,
                  online_ip_".$online['dpd_ip_type'].",
                  online_timestamp,
                  online_url
              )
          VALUES
          (
              '".$online['user_id']."',
              '".$online['user_name']."',
              '".$online['dpd_ip']."',
              '".$online['dpd_time']."',
              '".$online['url']."'
          )
          "
      );
      
      /* Output the users from the last 15 minutes */
      $get_online_users = pdo("SELECT online_user_id, online_user_name, online_timestamp FROM online".$online['get_online_users_where']);
      pdo('commit');
      

      当然,这是表格(online_ip_v4和online_ip_v6是唯一的):

      enter image description here

      欢迎任何建议......

1 个答案:

答案 0 :(得分:0)

请提供SHOW CREATE TABLE online;,以便我们查看您拥有的索引(如果有)等。

使用赋值语句,而不是define(...)

使用绑定而不是连接 - 您很容易受到“SQL注入”的攻击。<​​/ p>

将DELETE分成两个(或三个)DELETE - OR的每一个DELETE。 (或者不能很好地优化。)

您可能需要

INDEX(online_timestamp)
UNIQUE(online_ip_v4)
UNIQUE(online_ip_v6)
INDEX(user_id)

对于没有IPv4和IPv6的网站,您会怎么做?

请提供“show online users”的查询/查询。