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 }

  

  

php统计IP PV和今日访问量统计方法的更多相关文章

  1. 网站统计IP PV UV实现原理

    网站流量统计可以帮助我们分析网站的访问和广告来访等数据,里面包含很多数据的,比如访问试用的系统,浏览器,ip归属地,访问时间,搜索引擎来源,广告效果等.原来是一样的,这次先实现了PV,UV,IP三个重 ...

  2. 网站统计IP PV UV

    ###我只是一个搬运工 网站流量统计可以帮助我们分析网站的访问和广告来访等数据,里面包含很多数据的,比如访问使用的系统,浏览器,ip归属地,访问时间,搜索引擎来源,广告效果等. PV(访问量):Pag ...

  3. PHP文件操作,多行句子的读取,file()函数,file_get_contents()函数,file_put_contents()函数,is_file,统计网站pv (访问量),文件的复制 copy,文件重命名 rename,删除文件 unlink

    php中添加utf-8: header("Content-type:text/html;charset='UTF-8'"); 文件操作步骤: 1.在同一目录下建立一个file.tx ...

  4. shell-002:统计IP访问量

    统计IP访问量 #!/bin/bash # 统计IP的访问量 # 第一步首先得获取到日志的IP # 第二步给IP排序,这样相同的的IP就会在一起 sort # 第三步则给重复的IP统计数量,去重 un ...

  5. linux服务器上nginx日志访问量统计命令

    linux服务器上nginx日志访问量统计命令 日志文件所在地方:/var/log/nginx/access_iqueendress.com.log/var/log/nginx/access_m.iq ...

  6. 网站流量统计之PV和UV

    转自:http://blog.csdn.NET/webdesman/article/details/4062069 如果您是一个站长,或是一个SEO,您一定对于网站统计系统不会陌生,对于SEO新手来说 ...

  7. nginx访问量统计 日常分析

    nginx访问量统计 0.查询某个时间段的日志 cat appapi.dayutang.cn.access.log |grep 'POST'|grep '2019:10' > 20191059. ...

  8. ASP.net中网站访问量统计方法代码(在线人数,本月访问,本日访问,访问流量,累计访问)

    一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime),些表的信 ...

  9. 100个Shell脚本——【脚本9】统计ip

    [脚本9]统计ip 有一个日志文件,日志片段:如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com "/ ...

随机推荐

  1. mysql中in的用法详解

    一.基础用法 mysql中in常用于where表达式中,其作用是查询某个范围内的数据. select * from where field in (value1,value2,value3,-) 当 ...

  2. 7 个有趣的 Python 实战项目,超级适合练手

    关于Python,有一句名言:不要重复造轮子. 但是问题有三个: 1.你不知道已经有哪些轮子已经造好了,哪个适合你用.有名有姓的的著名轮子就400多个,更别说没名没姓自己在制造中的轮子. 2.确实没重 ...

  3. 使用高斯Redis实现二级索引

    摘要:高斯Redis 搭建业务二级索引,低成本,高性能,实现性能与成本的双赢. 本文分享自华为云社区<华为云GaussDB(for Redis)揭秘第21期:使用高斯Redis实现二级索引> ...

  4. Object类中wait带参方法和notifyAll方法和线程间通信

    notifyAll方法: 进入到Timed_Waiting(计时等待)状态有两种方式: 1.sleep(long m)方法,在毫秒值结束之后,线程睡醒,进入到Runnable或BLocked状态 2. ...

  5. 本机通过IP地址连接Ubuntu18.04+ on Vmware

    一.Vmware-顶部菜单栏-编辑-虚拟网络编辑器: 点一下 添加一个NAT模式的网络:要记住名称,比如这里我的是VMnet8 子网ip可以自己写,建议全程就都按我这个写,后续方便校对. 点一下 NA ...

  6. java使用配置skywalking

    一 .elasticsearch  和elasticsearch-head 1.下载 elasticsearch-6.3.1  ,下载地址 :https://pan.baidu.com/s/1ymxy ...

  7. 绿色安装MySQL5.7版本----配置my.ini文件注意事项

    前言 由于前段时间电脑重装,虽然很多软件不在C盘,但是由于很多注册表以及关联文件被删除,很多软件还需要重新配置甚至卸载重装. 使用MySQL时就遇到了这种情况,在修改配置文件无效的情况下选择了重新安装 ...

  8. 我的sql没问题为什么还是这么慢|MySQL加锁规则

    前言 前阵子参与了字节跳动后端青训营,其中大项目编写涉及到数据持久化一般选择使用MySQL.由于时间原因,数据库使用我选择了无脑三板斧:1. 建立了索引加速查询.2. 关闭自动提交事务.3. 在需要确 ...

  9. 【做题笔记】CF Edu Round 132

    1. 前言 本人第一次把 Div2. D 切了,开心. C 不会,寄. 后来在场外想到一种奇怪做法 AC 了. 2. 正文(A-D) CF 比赛链接 A. Three Doors 签到题.循环查找手中 ...

  10. Sharding-jdbc 5.1.2案例

    简介 sharding-jdbc案例,版本5.1.2 springboot + mybatis-plus + sharding-jdbc 项目地址:sharding-jdbc-example 模块说明 ...