2

I need to gather the IP addresses of visitors who view the home page of the site. There are bots that bounce off of the home page thousands of times a day. The Statistics module makes the site too slow. I also tried the Visitors module which worked pretty well, but there was no way to reduce the results only for the home page. Is there an another way to exclusively view IP addresses that visit the home page?

Edit: A way that doesn't slow the site down with a lot of database writing.

4 Answers 4

0

Here are the three variables you need :

$client  = $_SERVER['HTTP_CLIENT_IP'];
$forward = $_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = $_SERVER['REMOTE_ADDR'];
1
  • 1
    I think I am going to write a module that uses Drupal's ip_address() function. It uses these variables.
    – stodd42
    Commented Sep 23, 2015 at 13:56
0

If you are using Apache, all page requests are recorded in the log directory. On a Ubuntu machine the location of this directory will be /var/log/apache2.

To block spam traffic, I recommend the mod_spamhaus module. This link contains information on how to setup mod_spamhaus on a Ubuntu machine. The module checks for blacklisted IPs listed on the Spamhaus project and subsequently refuses the connection if a positive match is found.

1
  • This site doesn't run on Apache, I believe its nginx
    – stodd42
    Commented Sep 21, 2015 at 21:00
0

Update: For this project I ended up using the ip_address() function from Drupal's core to get the ip address. I then created a small table with some restrictions to prevent too much data from being written.

<?php

/**
 * Implements hook_init()
 */
function MY_MODULE_init() {
if(drupal_is_front_page() && user_is_anonymous() && ip_address() !== 'our ip address') {
    $ip = ip_address();
    $ipdate  = time();


    $insert = db_insert('mytable')->fields(array(
            'ip_address' => $ip,
            'ip_date' => $ipdate,
        ))->execute();
    }
}

function MY_MODULE_menu() {
  $items = array();
  $items['view-results'] = array(
    'title'           => 'View List of IP Addresses',
    'page callback'   => 'MY_MODULE_fetch_results',
    'access callback' => 'user_is_logged_in',
    'type'            => MENU_CALLBACK,
  );         
  return $items;
}

function MY_MODULE_fetch_results() {
$result = db_query("SELECT ip_address AS IPADDRESS, ip_date
                    AS IPDATE 
                    FROM mytable 
                    ORDER BY IPDATE DESC");
print "<title>List of IP Addresses</title>";
print "<h3 style='font-family:verdana, arial'>List of IP Addresses and Dates for Anonymous Users Home Page Visits</h3>";
print "<p style='font-family:verdana, arial; font-size:11px'>Hint: use CTRL + F to find</p>";
print "<div style='font-weight:bold; height:18px; background-color:#f5f1ec; font-family:verdana, arial'>
   <div style='width:45%; float:left;'>Ip Address</div>
    <div style='width:45%; float:left;'>Date of Visit</div>";
  foreach($result as $data) {
      print "<div style='font-weight:normal; background-color:#efefef; font-family:verdana, arial; height:18px; margin:4px;font-size:11px'>";
       print"<div style='width:45%; float:left;'>". $data->IPADDRESS . "</div>";
       printf("<div style='width:45%%; float:left; color:#336699;'>%s</div></div><div style='clear:both;'></div>", format_date($data->IPDATE, 'custom', 'j F Y g:i A'));
  }
drupal_exit();
}
0
  1. Enable Drupals built-in Database logging module. To view the log, navigate to the Recent log messages page (http://example.com/admin/reports/dblog). However, I don’t see any reason why you should fill up your Drupal database with thousands of log entries, thus

  2. Use a graphical analytics Tool like Piwik or GoogleAnalytics

  3. Enable logging in your Apache (/var/log/apache2) or nginx Webserver. Additionally to analyze your log files more easily you can use

  4. Awstats or Report Magic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.