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. 打通web的三维国产引擎!老子云AMRT,够牛!

    AMRT(Auto Mobile Reality Technology)指的是自动化移动现实技术,它是老子云3D模型自动轻量化引擎及轻量化模型格式.模型展示框架.API/SDK的统称.3D研发技术其中 ...

  2. Eclipse历史版本下载和选择对应的java版本

    下载Eclipse 官网: https://www.eclipse.org/ 直达 直接进入连接:https://www.eclipse.org/downloads/packages/installe ...

  3. 【故障公告】取代 memcached 的 redis 出现问题造成网站故障

    6月19日开始,我们将博客站点的缓存服务器从 memcached 换成了 redis,稳定运行了3天,今天上午访问高峰突然出现问题,在 11:00-12:30 期间影响了网站的正常访问,由此给您带来麻 ...

  4. UiPath条件判断活动If的介绍和使用

    if的介绍 if语句是指编程语言(包括c语言.C#.Python.Java.汇编语言等)中用来判定所给定的条件是否满足,根据判定的结果(真或假)决定执行给出的两种操作之一. if在UiPath中的使用 ...

  5. 服务器与Ajax

    前端相关的技术点 HTML   主要用来实现页面的排版布局 CSS   主要用来实现页面的样式美化 JavaScript   主要用来实现前端功能特效 Ajax基础知识铺垫 客户端与服务器 通信协议( ...

  6. 有关golang信道的面试笔记

    信道是一个goroutine之间很关键的通信媒介. 理解golang的信道很重要,这里记录平时易忘记的.易混淆的点. 1. 基本使用 刚声明的信道,零值为nil,无法直接使用,需配合make函数进行初 ...

  7. 159_模型_Power BI 地理分析之形状地图

    159_模型_Power BI 地理分析之形状地图 声明以下地图元素仅供学习交流所用,如需地图公开使用请提前做好报审工作. 一.背景 当企业的体量达到一定体量的时候,保持稳定的增长是非常重要的事情.本 ...

  8. Web || Html_Css_JS

    第三阶段课程介绍: web前端 l 数据库 l SpringBoot Web前端-HTML l HTML作用: 负责搭建页面结构和内容 (盖房子) l 学习HTML主要学习的就是有哪些标签 文本相关标 ...

  9. docker容器管理操作

    Docker容器的四种状态: 运行 已暂停 重新启动 已退出 1.容器的创建 容器创建:就是将镜像加载到容器的过程. 创建容器时如果没有指定容器名称,系统会自动创建一个名称. 新创建的容器默认处于停止 ...

  10. Keyboading 思路

    0x01 前置芝士 还是先放个 link 吧. 所需知识点:BFS. 思维难度较高,实现简单. 0x02 题目大意:其实就是给你个图,按顺序走到相应的点,求所需最少步数(走到需要去的点会耗费一次步数) ...