--- modules/statistics.module	Sun Jul 25 17:17:32 2004
+++ statistics.module	Sun Jul 25 17:17:09 2004
@@ -260,20 +260,32 @@
  *   - "all": Display all referrers.
  */
 function statistics_top_refer($view = 'all') {
+  //
+  // $_SERVER['HTTP_HOST'] may contain a trailing port designation.
+  // For example: http://www.domain.tld:80
+  // If this is the case, then MySQL will not properly distinguish
+  // between internal and external referers.
+  //
+  $http_host = $_SERVER['HTTP_HOST'];
+  $colon_position = strrpos($http_host, ':');
+  if ($colon_position !== FALSE) {
+    $http_host = substr($http_host, 0, $colon_position-1);
+  }
+
   if ($view == 'all') {
     $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url <> '' GROUP BY url";
     $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> ''";
     $describe = t('Top referrers of the past %interval');
   }
   elseif ($view == 'internal') {
-    $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%' GROUP BY url";
-    $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%'";
+    $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url LIKE '%". check_query($http_host) ."%' GROUP BY url";
+    $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url LIKE '%". check_query($http_host) ."%'";
     $describe = t('Top internal referrers of the past %interval');
   }
   else {
     /* default to external */
-    $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url NOT LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%' AND url <> '' GROUP BY url";
-    $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%'";
+    $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url NOT LIKE '%". check_query($http_host) ."%' AND url <> '' GROUP BY url";
+    $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%". check_query($http_host) ."%'";
     $describe = t('Top external referrers of the past %interval');
   }
 
