简体   繁体   English

跟踪IP地址

[英]Tracking an IP address

I want to track IP addresses of visitors to my blog. 我想跟踪博客访问者的IP地址。 I don't know which blog I'm going to use, I'll use whichever one will work (i hear blogger doesn't work with php). 我不知道我要使用哪个博客,我将使用任何一个可以工作的博客(我听说博客作者不适用于php)。

Also, once I make the blog and set up the IP tracker, where will I go to find the IP addresses of my visitors? 此外,一旦创建博客并设置IP跟踪器,我将在哪里找到访问者的IP地址? Thanks in advance! 提前致谢!

You can check the access log of your http server. 您可以检查http服务器的访问日志。 This should give you a list of client requests. 这应该为您提供客户请求列表。

If your looking for a php solution, you can use the following to get the ip address of the client: 如果您正在寻找php解决方案,则可以使用以下命令获取客户端的ip地址:

 $_SERVER['REMOTE_ADDR'];

You'll need to write a quick logging script to store these 您需要编写一个快速日志记录脚本来存储这些

$logFile = 'iplog.log';
if(!file_exists($logFile)) touch($logFile);
if(is_writable($logFile)) {
  $fh = fopen($logFile, 'a');
  if($fh) {
    $line = $_SERVER['REMOTE_ADDR']."\n";
    fwrite($fh, $line, strlen($line));
    fclose($fh);
  }
}

If later on you would like to use those IPs as for example in admin area of your blog, then IMHO it is better to store them in database. 如果以后您想在博客的管理区域中使用这些IP,那么恕我直言,最好将它们存储在数据库中。 Later on you can cache them, but it is optional. 以后您可以缓存它们,但这是可选的。

In WordPress (which is by the way very elastic blog system) database tables have wp_ prefix by default. 在WordPress(这是非常灵活的博客系统)中,数据库表默认具有wp_前缀。 So you could do something like that. 所以你可以做类似的事情。

CREATE TABLE IF NOT EXISTS wp_ip_tracking (
    id INT NOT NULL AUTO_INCREMENT,
    ip VARCHAR(15) NOT NULL,
    last_activity TIMESTAMP NOT NULL,
    PRIMARY KEY(id),
    UNIQUE(ip)
);

Then you could do some function which will be called, when member does pretty much anything. 然后,当成员几乎执行任何操作时,您可以执行一些将被调用的函数。 Depends on what you need. 取决于您的需求。

function trackIP($ip) {
    // Check if IP exists
    $query1 = "SELECT id FROM wp_ip_tracking WHERE ip = '{$ip}'";
    // Insert new record with given IP
    $query2 = "INSERT INTO wp_ip_tracking(id, ip, last_activity) VALUES(NULL, '{$ip}', NOW())";
    // Update record for specified IP
    $query3 = "UPDATE wp_ip_tracking SET last_activity = NOW() WHERE ip = '{$ip}'";

    if(mysql_num_rows(mysql_query($query1)) == 0) {
        mysql_query($query2);
    } else {
        mysql_query($query3);
    }
}

I think that those two should help you with your problem. 我认为这两个应该可以帮助您解决问题。 Again it is only IMHO. 同样,这只是恕我直言。

您可以在此站点上注册..此站点是跟踪ips的好工具.. http://fcounter.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM