php统计IP PV和今日访问量

php统计IP方法:

php引用,在wordpress主题中

$getroot=$_SERVER['DOCUMENT_ROOT'];

require_once("$getroot/countstart.php");

function getIpAddress() { // 取得当前用户的IP地址
	$ip = '127.0.0.1';
    if(isset($_SERVER)){
        if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }else if(isset($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }else{
            $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{
            $ip = getenv("REMOTE_ADDR");
        }
    }
    return $ip;
}
function writeover($filename, $data, $method = 'w', $chmod = 0) {
	$handle = fopen ( $filename, $method );
	! handle && die ( "文件打开失败" );
	flock ( $handle, LOCK_EX );
	fwrite ( $handle, $data );
	flock ( $handle, LOCK_UN );
	fclose ( $handle );
	$chmod && @chmod ( $filename, 0777 );
}
function count_online_num($time, $ip) {
	$fileCount = './count.txt';
	$count = 0;
	$gap = 900; // 15分钟页面不刷新清空对应IP
	if (! file_exists ( $fileCount )) {
		$str = $time . "\t" . $ip . "\r\n";
		writeover ( $fileCount, $str, 'w', 1 );
		$count = 1;
	} else {
		$arr = file ( $fileCount );
		$flag = 0;
		foreach ( $arr as $key => $val ) {
			$val = trim ( $val );
			if ($val != "") {
				list ( $when, $seti ) = explode ( "\t", $val );
				if ($seti == $ip) {
					$arr [$key] = $time . "\t" . $seti;
					$flag = 1;
				} else {
					$currentTime = time ();
					if ($currentTime - $when > $gap) {
						unset ( $arr [$key] );
					} else {
						$arr [$key] = $val;
					}
				}
			}
		}
		if ($flag == 0) {
			array_push ( $arr, $time . "\t" . $ip );
		}
		$count = count ( $arr );
		$str = implode ( "\r\n", $arr );
		$str .= "\r\n";
		writeover ( $fileCount, $str, 'w', 0 );
		unset ( $arr );
	}
	return $count;
}
$time = time ();
$ip = getIpAddress ();
$online_num = count_online_num ( $time, $ip );
echo $online_num;

访问PV、今日访问量、在线人数使用方法:

javascript引用:

<script src="count.php"></script>
class minicount
{
	var $dataPth;
	function __construct($dataPth = "minidata/")
	{
		$this->dataPath = $dataPth;
	}
	//解决PHP4不支持__construct问题
	function minicount($dataPth = "minidata/")
	{
		$this->dataPath = $dataPth;
	}
	function diplay($format="a%t%y%m%l%"){
		//echo $format;
		$this->updateCount($this->dataPath.$this->getMainFileName());		//更新总流量
		$this->updateCount($this->dataPath.$this->getTodayFileName());		//更新今天流量
		$this->updateCount($this->dataPath.$this->getMonthFileName());		//更新今月流量
		$search = array("'a%'i",
			"'t%'i",
			"'y%'i",
			"'m%'i",
			"'l%'i",
		);
		$replace = array($this->getCount($this->dataPath.$this->getMainFileName()),
			$this->getCount($this->dataPath.$this->getTodayFileName()),
			$this->getCount($this->dataPath.$this->getYesterdayFileName()),
			$this->getCount($this->dataPath.$this->getMonthFileName()),
			$this->getCount($this->dataPath.$this->getLastMonthFileName())
		);
		echo preg_replace ($search, $replace, $format);
	}
	function updateCount($f)
	{
		//echo $this->dataPath;
		$handle = fopen($f, "a+") or die("找不到文件");
		$counter = fgets($handle,1024);
		$counter = intval($counter);
		$counter++;
		fclose($handle);
		//写入统计
		$handle = fopen($f, "w") or die("打不开文件");
		fputs($handle,$counter) or die("写入失败");
		fclose($handle);
	}
	function getCount($f)
	{
		$handle = fopen($f, "a+") or die("找不到文件");
		$counter = fgets($handle,1024);
		$counter = intval($counter);
		fclose($handle);
		return $counter;
	}
	function getMainFileName()
	{
		return "counter.txt";
	}
	function getYesterdayFileName()
	{
		return "day/".date("Ymd",strtotime('-1 day')).".txt";
	}
	function getTodayFileName()
	{
		return "day/".date("Ymd").".txt";
	}
	function getMonthFileName()
	{
		return "month/".date("Ym").".txt";
	}
	function getLastMonthFileName()
	{
		return "month/".date("Ym",strtotime('-1 month')).".txt";
	}
}
//require_once(dirname(__FILE__)."/count.php");
$c = new minicount(dirname(__FILE__)."/countdata/");
$filename = dirname(__FILE__)."/countdata/online.txt";  //数据文件
$cookiename = 'VGOTCN_OnLineCount';  //cookie名称
$onlinetime = 1800;  //在线有效时间,单位:秒 (即1800等于30分钟)
$online = file($filename); 
$nowtime = time(); 
$nowonline = array();
foreach($online as $line) {
	$row = explode('|',$line);
	$sesstime = trim($row[1]);
	if(($nowtime - $sesstime) <= $onlinetime) { $nowonline[$row[0]] = $sesstime; } } if(isset($_COOKIE[$cookiename])) { $uid = $_COOKIE[$cookiename]; } else { $vid = 0; do { $vid++; $uid = 'U'.$vid; } while (array_key_exists($uid,$nowonline)); setcookie($cookiename,$uid); } $nowonline[$uid] = $nowtime; $total_online = count($nowonline); if($fp = @fopen($filename,'w')) { if(flock($fp,LOCK_EX)) { rewind($fp); foreach($nowonline as $fuid => $ftime) {
			$fline = $fuid.'|'.$ftime."\n";
			@fputs($fp,$fline); 
		}
		flock($fp,LOCK_UN);
		fclose($fp);
	}
}
//$c->diplay("总浏览量:a% 人次 今天访问:t%人次 昨天访问:y%人次 今月访问:m%人次 上月访问:l%人次");
//$c->diplay("document.writeln('总浏览量:a%人次 今天访问:t%人次 昨天访问:y%人次 当前在线人数:{$total_online}人')");
$c->diplay("document.writeln('总浏览量:a% 人次  今日访问:t% 人次  在线人数:{$total_online} 人  开始统计日期:2020-03-04')");
//总访问量:xxx;  总浏览量:777 人次  今日访问:  开始统计日期:2020-03-04 
//使用方法:

本文固定链接: http://blog.wmphp.com/3134.html | 顺盛网络博客

该日志由 admin 于2018年03月10日发表在 PHP 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php统计IP PV和今日访问量 | 顺盛网络博客
【上一篇】
【下一篇】

php统计IP PV和今日访问量:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter