普通的计数器网上有很多了。但是,我们要实现根据ip来计数。比如,上级下发的文件,我们要根据ip来统计下级有没有查看,执行呢?很简单的。

<?php
function getIP() {
    if ($_SERVER["HTTP_X_FORWARDED_FOR"])
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else if ($_SERVER["HTTP_CLIENT_IP"])
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    else if ($_SERVER["REMOTE_ADDR"])
        $ip = $_SERVER["REMOTE_ADDR"];
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "Unknown";
    return $ip;
}
?>
<?php
$ip=getIP();
function makeNum(){
    global $ip;
 if(!file_exists(''.$ip.'.txt')){
  $fp=fopen(''.$ip.'.txt','w');
  fwrite($fp,"1");
  fclose($fp);
  $n=1;
 }
 else{
  $fp=fopen(''.$ip.'.txt','r');
  $n=fgets($fp);
  fclose($fp);
  $n++;
  $fp=fopen(''.$ip.'.txt','w');
  fwrite($fp,$n);
  fclose($fp);
 }
 return $n;
}
$guestsNum=makeNum();
echo "已查看".$guestsNum."次数";
?>