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. JavaScript 语言入门

    目录 JavaScript 介绍 JavaScript 和 和 html 代码的结合方式 第一种方式 第二种方式 4.变量 关系(比较)运算 逻辑运算 数组(重点) 函数(重点) 函数的二种定义方式 ...

  2. Python Socket Sever

    1. Server code 1 # !/usr/bin/env python 2 # coding:utf-8 3 import multiprocessing 4 import socket 5 ...

  3. 简单实现python接口自动化(一)

    目的:excel中维护接口用例数据,通过python中requests库进行读取用例,并把运行结果与excel中的预期结果对比,最后把执行情况写入到excel中去. excel维护数据: 具体的接口名 ...

  4. 基于POM---UI测试框架

    为什么会出现这个半自动化UI测试框架 我进入公司的前一个月从事的手工测试,为了提高自己的测试效率在工作时间之外写了一个半自动化的UI测试(害怕手工测试做久了,忘记自己还学过软件开发), 为什么我把它叫 ...

  5. Tapdata Cloud 版本上新 | 支持通知自配置,支持GP、MQ数据源,界面更友好!

    Tapdata Cloud https://cloud.tapdata.net Tapdata Cloud 是国内首家异构数据库实时同步云平台,目前支持Oracle.MySQL.PG.SQL Serv ...

  6. 阿里 Maven仓库

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  7. Redis相关练习操作,redis连接池

    package com.shujia.controller; import com.shujia.entity.MyTypedTuple; import com.shujia.entity.User; ...

  8. 循环队列(严3.30)--------西工大NOJ习题.9

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct _Q ...

  9. 乘风破浪,遇见未来新能源汽车(Electric Vehicle)之特斯拉提车必须知道的十个流程

    订车 线下门店或者官网可以咨询和下单,一般来说,订金就是1000,还算可以接受. 订单账号 特斯拉账号是以邮箱为区分的,而不是手机号,我们下单的时候需要提供一个邮箱用来注册特斯拉账号. 注意了,敲黑板 ...

  10. 十分钟教会你如何使用VitePress搭建及部署个人博客站点

    使用VitePress可以让我们快速搭建一个静态博客网站,这篇文章将带领大家搭建一个基于VitePress的静态博客网站并且部署到GitHub Pages(github提供的静态网页服务) 快速上手 ...