php统计IP PV和今日访问量统计方法

php引用,在wordpress主题中

$getroot=$_SERVER[‘DOCUMENT_ROOT’];
require_once(“$getroot/countstart.php”);

 1 function getIpAddress() { // 取得当前用户的IP地址
 2     $ip = '127.0.0.1';
 3     if(isset($_SERVER)){
 4         if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
 5             $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
 6         }else if(isset($_SERVER["HTTP_CLIENT_IP"])) {
 7             $ip = $_SERVER["HTTP_CLIENT_IP"];
 8         }else{
 9             $ip = $_SERVER["REMOTE_ADDR"];
10         }
11     }else{
12         if(getenv("HTTP_X_FORWARDED_FOR")){
13             $ip = getenv("HTTP_X_FORWARDED_FOR");
14         }else if(getenv("HTTP_CLIENT_IP")) {
15             $ip = getenv("HTTP_CLIENT_IP");
16         }else{
17             $ip = getenv("REMOTE_ADDR");
18         }
19     }
20     return $ip;
21 }
22 function writeover($filename, $data, $method = 'w', $chmod = 0) {
23     $handle = fopen ( $filename, $method );
24     ! handle && die ( "文件打开失败" );
25     flock ( $handle, LOCK_EX );
26     fwrite ( $handle, $data );
27     flock ( $handle, LOCK_UN );
28     fclose ( $handle );
29     $chmod && @chmod ( $filename, 0777 );
30 }
31 function count_online_num($time, $ip) {
32     $fileCount = './count.txt';
33     $count = 0;
34     $gap = 900; // 15分钟页面不刷新清空对应IP
35     if (! file_exists ( $fileCount )) {
36         $str = $time . "\t" . $ip . "\r\n";
37         writeover ( $fileCount, $str, 'w', 1 );
38         $count = 1;
39     } else {
40         $arr = file ( $fileCount );
41         $flag = 0;
42         foreach ( $arr as $key => $val ) {
43             $val = trim ( $val );
44             if ($val != "") {
45                 list ( $when, $seti ) = explode ( "\t", $val );
46                 if ($seti == $ip) {
47                     $arr [$key] = $time . "\t" . $seti;
48                     $flag = 1;
49                 } else {
50                     $currentTime = time ();
51                     if ($currentTime - $when > $gap) {
52                         unset ( $arr [$key] );
53                     } else {
54                         $arr [$key] = $val;
55                     }
56                 }
57             }
58         }
59         if ($flag == 0) {
60             array_push ( $arr, $time . "\t" . $ip );
61         }
62         $count = count ( $arr );
63         $str = implode ( "\r\n", $arr );
64         $str .= "\r\n";
65         writeover ( $fileCount, $str, 'w', 0 );
66         unset ( $arr );
67     }
68     return $count;
69 }
70 $time = time ();
71 $ip = getIpAddress ();
72 $online_num = count_online_num ( $time, $ip );
73 echo $online_num;

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

javascript引用:

<script src="count.php"></script>

 1 class minicount
 2 {
 3     var $dataPth;
 4     function __construct($dataPth = "minidata/")
 5     {
 6         $this->dataPath = $dataPth;
 7     }
 8     //解决PHP4不支持__construct问题
 9     function minicount($dataPth = "minidata/")
10     {
11         $this->dataPath = $dataPth;
12     }
13     function diplay($format="a%t%y%m%l%"){
14         //echo $format;
15         $this->updateCount($this->dataPath.$this->getMainFileName());        //更新总流量
16         $this->updateCount($this->dataPath.$this->getTodayFileName());        //更新今天流量
17         $this->updateCount($this->dataPath.$this->getMonthFileName());        //更新今月流量
18         $search = array("'a%'i",
19             "'t%'i",
20             "'y%'i",
21             "'m%'i",
22             "'l%'i",
23         );
24         $replace = array($this->getCount($this->dataPath.$this->getMainFileName()),
25             $this->getCount($this->dataPath.$this->getTodayFileName()),
26             $this->getCount($this->dataPath.$this->getYesterdayFileName()),
27             $this->getCount($this->dataPath.$this->getMonthFileName()),
28             $this->getCount($this->dataPath.$this->getLastMonthFileName())
29         );
30         echo preg_replace ($search, $replace, $format);
31     }
32     function updateCount($f)
33     {
34         //echo $this->dataPath;
35         $handle = fopen($f, "a+") or die("找不到文件");
36         $counter = fgets($handle,1024);
37         $counter = intval($counter);
38         $counter++;
39         fclose($handle);
40         //写入统计
41         $handle = fopen($f, "w") or die("打不开文件");
42         fputs($handle,$counter) or die("写入失败");
43         fclose($handle);
44     }
45     function getCount($f)
46     {
47         $handle = fopen($f, "a+") or die("找不到文件");
48         $counter = fgets($handle,1024);
49         $counter = intval($counter);
50         fclose($handle);
51         return $counter;
52     }
53     function getMainFileName()
54     {
55         return "counter.txt";
56     }
57     function getYesterdayFileName()
58     {
59         return "day/".date("Ymd",strtotime('-1 day')).".txt";
60     }
61     function getTodayFileName()
62     {
63         return "day/".date("Ymd").".txt";
64     }
65     function getMonthFileName()
66     {
67         return "month/".date("Ym").".txt";
68     }
69     function getLastMonthFileName()
70     {
71         return "month/".date("Ym",strtotime('-1 month')).".txt";
72     }
73 }
74 //require_once(dirname(__FILE__)."/count.php");
75 $c = new minicount(dirname(__FILE__)."/countdata/");
76 $filename = dirname(__FILE__)."/countdata/online.txt";  //数据文件
77 $cookiename = 'VGOTCN_OnLineCount';  //cookie名称
78 $onlinetime = 1800;  //在线有效时间,单位:秒 (即1800等于30分钟)
79 $online = file($filename); 
80 $nowtime = time(); 
81 $nowonline = array();
82 foreach($online as $line) {
83     $row = explode('|',$line);
84     $sesstime = trim($row[1]);
85     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) {
86             $fline = $fuid.'|'.$ftime."\n";
87             @fputs($fp,$fline); 
88         }
89         flock($fp,LOCK_UN);
90         fclose($fp);
91     }
92 }

 提示,获取IP方法:

 1 function get_client_ip($type = 0) {
 2     $type       =  $type ? 1 : 0;
 3     static $ip  =   NULL;
 4     if ($ip !== NULL) return $ip[$type];
 5     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 6         $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
 7         $pos    =   array_search('unknown',$arr);
 8         if(false !== $pos) unset($arr[$pos]);
 9         $ip     =   trim($arr[0]);
10     }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
11         $ip     =   $_SERVER['HTTP_CLIENT_IP'];
12     }elseif (isset($_SERVER['REMOTE_ADDR'])) {
13         $ip     =   $_SERVER['REMOTE_ADDR'];
14     }
15     // IP地址合法验证
16     $long = sprintf("%u",ip2long($ip));
17     $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
18     return $ip[$type];
19 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑆箫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值