**''getRealIpAddr''** is a PHP function to try as best to get the IP address from the visitor.\\

A simple colon-separated text file is then read with the last visitor IP, if it doesn't match, the counts for both the visitors today plus the visitors yesterday and all days are incremented and stored back in the file. The date of the file when it was read is displayed also as the date of the last visitor.\\
<code>
<?php
function getRealIpAddr()  {
$ip=''; 
if (!empty($_SERVER['HTTP_CLIENT_IP']))  {  
$ip=$_SERVER['HTTP_CLIENT_IP'];  
}  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
//to check ip is pass from proxy  
{  
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];  
}  else  {  
$ip=$_SERVER['REMOTE_ADDR'];  }  
return $ip;  
}

$cfile = "/home/mysite/public_html/count.txt";

// Read old entries
$fp = fopen($cfile, "r");
$buffer=fgets($fp, 4096);
fclose ($fp);

$ip = chop (substr($buffer,0,15));
//counts are total, today, yesterday
$counts = explode(':',substr($buffer,15));
$np = getRealIpAddr();
$date = date("d-M-Y @ H:i", filemtime($cfile));
$fdate = substr($date,0,11);
$today = date("d-M-Y");
settype($counts[0], "integer");
settype($counts[1], "integer");
settype($counts[2], "integer");

if ($np != $ip) {
 $counts[0]++;
 if ($fdate != $today) {
  $counts[2] = $counts[1];
  $counts[1] = 1;
 } else { $counts[1]++; }
 if ($fp = fopen ($cfile, "w")) {
  fwrite ($fp, substr($np."         ",0,15).$counts[0].':'.$counts[1].':'.$counts[2]);
  fclose ($fp);
 }
}

echo "<font color=\"#FF9933\"><b>$counts[0]</b></font> total visitors<br />\n";
echo "<font color=\"#6699FF\"><b>$counts[1]</b></font> visitors today $today<br />\n";
echo "<font color=\"#EF33EF\"><b>$counts[2]</b></font> total visitors previously<br />\n";
echo "Last visitor IP [<font color=\"#00CC00\">$ip</font>]<br />\n";
echo "Date of last visit<br />\n[<font color=\"#3333FF\">";
echo $date."</font>]<br />\n";
echo "Current visitor IP [<font color=\"#FF00CC\">$np</font>]<br />\n";
?>
</code>

For MovableType, I create a module template called **Visitor Counter**\\
<code>
<?php
function getRealIpAddr()  {
$ip=''; 
if (!empty($_SERVER['HTTP_CLIENT_IP']))  {  
$ip=$_SERVER['HTTP_CLIENT_IP'];  
}  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
//to check ip is pass from proxy  
{  
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];  
}  else  {  
$ip=$_SERVER['REMOTE_ADDR'];  }  
return $ip;  
}

$cfile = "<$mt:BlogSitePath$>count.txt";

// Read old entries
$fp = fopen($cfile, "r");
$buffer=fgets($fp, 4096);
fclose ($fp);

$ip = chop (substr($buffer,0,15));
//counts are total, today, yesterday
$counts = explode(':',substr($buffer,15));
$np = getRealIpAddr();
$date = date("d-M-Y @ H:i", filemtime($cfile));
$fdate = substr($date,0,11);
$today = date("d-M-Y");
settype($counts[0], "integer");
settype($counts[1], "integer");
settype($counts[2], "integer");

if ($np != $ip) {
 $counts[0]++;
 if ($fdate != $today) {
  $counts[2] = $counts[1];
  $counts[1] = 1;
 } else { $counts[1]++; }
 if ($fp = fopen ($cfile, "w")) {
  fwrite ($fp, substr($np."         ",0,15).$counts[0].':'.$counts[1].':'.$counts[2]);
  fclose ($fp);
 }
}

echo "<div class=\"counter\">\n";
echo "<font color=\"#FF9933\"><b>$counts[0]</b></font> total visitors<br />\n";
echo "<font color=\"#6699FF\"><b>$counts[1]</b></font> visitors today $today<br />\n";
echo "<font color=\"#EF33EF\"><b>$counts[2]</b></font> total visitors previously<br />\n";
echo "Last visitor IP [<font color=\"#00CC00\">$ip</font>]<br />\n";
echo "Date of last visit<br />\n[<font color=\"#3333FF\">";
echo $date."</font>]<br />\n";
echo "Current visitor IP [<font color=\"#FF00CC\">$np</font>]<br />\n";
echo "</div>\n";
?>
</code>

A more complete search for an IP address is\\
<code>
function getRealIpAddr()  {
$ip=''; 
if (getenv('HTTP_CLIENT_IP'))  
 $ip=getenv('HTTP_CLIENT_IP');
elseif (getenv('HTTP_X_FORWARDED_FOR'))  
 $ip=getenv('HTTP_X_FORWARDED');
elseif (getenv('HTTP_X_FORWARDED_FOR'))  
 $ip=getenv('HTTP_X_FORWARDED');
elseif (getenv('HTTP_FORWARDED_FOR'))  
 $ip=getenv('HTTP_FORWARDED_FOR');
elseif (getenv('HTTP_FORWARDED'))  
 $ip=getenv('HTTP_FORWARDED');
elseif (getenv('REMOTE_ADDR'))
 $ip=getenv('REMOTE_ADDR');
else $ip="UNKNOWN";  
return $ip;  
}
</code>

An older version I used that checks for invalid ips also
<code>
<?php
function validip($ip) {
   if (!empty($ip) && ip2long($ip)!=-1) {
       $reserved_ips = array (
       array('0.0.0.0','2.255.255.255'),
       array('10.0.0.0','10.255.255.255'),
       array('127.0.0.0','127.255.255.255'),
       array('169.254.0.0','169.254.255.255'),
       array('172.16.0.0','172.31.255.255'),
       array('192.0.2.0','192.0.2.255'),
       array('192.168.0.0','192.168.255.255'),
       array('255.255.255.0','255.255.255.255')
       );

       foreach ($reserved_ips as $r) {
           $min = ip2long($r[0]);
           $max = ip2long($r[1]);
           if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
       }
       return true;
   } else { 
       return false;
   }
}

function getip() {
   if (validip($_SERVER["HTTP_CLIENT_IP"])) {
       return $_SERVER["HTTP_CLIENT_IP"];
   }
   foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) {
       if (validip(trim($ip))) {
           return $ip;
       }
   }
   if (validip($_SERVER["HTTP_X_FORWARDED"])) {
       return $_SERVER["HTTP_X_FORWARDED"];
   } elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) {
       return $_SERVER["HTTP_FORWARDED_FOR"];
   } elseif (validip($_SERVER["HTTP_FORWARDED"])) {
       return $_SERVER["HTTP_FORWARDED"];
   } elseif (validip($_SERVER["HTTP_X_FORWARDED"])) {
       return $_SERVER["HTTP_X_FORWARDED"];
   } else {
       return $_SERVER["REMOTE_ADDR"];
   }
}
$cfile = "/home/example/public_html/count.txt";

// Read old entries
$fp = fopen($cfile, "r");
$buffer=fgets($fp, 4096);
fclose ($fp);

$ip = chop (substr($buffer,0,15));
//counts are total, today, yesterday
$counts = explode(':',substr($buffer,15));
$np = getip();
$date = date("d-M-Y @ H:i", filemtime($cfile));
$fdate = substr($date,0,11);
$today = date("d-M-Y");
settype($counts[0], "integer");
settype($counts[1], "integer");
settype($counts[2], "integer");

if ($np != $ip) {
 $counts[0]++;
 if ($fdate != $today) {
  $counts[2] = $counts[1];
  $counts[1] = 1;
 } else { $counts[1]++; }
 if ($fp = fopen ($cfile, "w")) {
  fwrite ($fp, substr($np."         ",0,15).$counts[0].':'.$counts[1].':'.$counts[2]);
  fclose ($fp);
 }
}

echo "<font color=\"#FF9933\"><b>$counts[0]</b></font> total visitors<br />\n";
echo "<font color=\"#6699FF\"><b>$counts[1]</b></font> visitors today $today<br />\n";
echo "<font color=\"#EF33EF\"><b>$counts[2]</b></font> total visitors previously<br />\n";
echo "Last visitor IP [<font color=\"#00CC00\">$ip</font>]<br />\n";
echo "Date of last visit<br />\n[<font color=\"#3333FF\">";
echo $date."</font>]<br />\n";
echo "Current visitor IP [<font color=\"#FF00CC\">$np</font>]<br />\n";

?>
</code>
