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
//使用方法:总浏览量:11370 人次 今日访问:48 人次 在线人数:1 人 开始统计日期:2020-03-04

END
如本资源侵犯了您的权益,请联系投诉邮箱admin@wmphp.com进行举报!我们将在收到邮件的1个小时内处理完毕。
本站仅为平台,发布的资源均为用户投稿或转载!所有资源仅供参考学习使用,请在下载后的24小时内删除,禁止商用!
Wmphp.com(完美源码)助力正版,如您有自己的原创产品,可以联系客服投稿,代理出售!
Wmphp.com(完美源码)客服QQ:136882447
Wmphp.com(完美源码)商务电话(仅对企业客户/个人用户):15120086569 (微信同步)
请注意:本站不提供任何免费的技术咨询服务,为了节约时间,下载前 请确认自己会技术
完美源码 » php统计IP PV和今日访问量
完美源码 » php统计IP PV和今日访问量