简体   繁体   中英

Tracking an IP address

I want to track IP addresses of visitors to my blog. 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).

Also, once I make the blog and set up the IP tracker, where will I go to find the IP addresses of my visitors? Thanks in advance!

You can check the access log of your http server. 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:

 $_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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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