<?php
require_once("mem.config.php"); //memcache 配置文件,包括memcache的远程地址、端口,登录名和密码等
require_once("conn.php"); //数据库 配置文件,包括数据库的host username password database等
header("Content-Type: text/html; charset=UTF-8");
define("STARTUP", 0); //初始记录的设定,方便作假哦,哈哈
define("TEMPFILE", "tempfile"); //用于缓存的临时文件
define("ONLINETNAME", "online"); //存储在线人信息的表名
define("SESSIONTIME",600); //在线判断时间,默认为session自动结束时间
define("STATICSTNAME","statics"); //统计信息表格
define("ACCESSTNAME",'ipaccess'); //访问量信息表格
class IpAccessStatistics
{
private $t_now;
private $t_array;
private $day;
private $mon;
private $year;
private $onlinefiletname;
private $total_c;
private $today_c;
private $yesterday_c;
private $your_c;
private $total_online;
private $maxaccesstime;
private $maxaccess;
private $link;
private $tempfile;
private $ipaccesstname;
private $staticstname;
private $mem;
public function __construct()
{
//看你安装的mem客户端插件是什么了memcache还是memcached
//$this->mem = new Memcached;
$this->mem = new Memcache;
$mem = $this->mem;
//$mem->AddServer(MEM_HOST,MEM_PORT);
$mem->connect(MEM_HOST,MEM_PORT);
$this->total_c = $mem->get("total_c");
//如果所有值在memcahce中已经缓存,则在memcache中取值进行计算
if(!empty($this->total_c))
{
$this->t_now = time();
$this->t_array = getdate($this->t_now);
$this->day = $this->t_array['mday'];
$this->mon = $this->t_array['mon'];
$this->year = $this->t_array['year'];
//获取memcache中的总访问量,今日访问量,昨日访问量,当前在线人数,开始人数,最大访问量,最大访问时间,最近访问统计时间
$this->total_c=$mem->get("total_c");
$this->today_c=$mem->get("today_c");
$this->yesterday_c=$mem->get("yesterday_c");
$this->your_c=$mem->get("your_c");
$this->total_online=$mem->get("total_online");
$this->startno = $mem->get("startno");
$this->maxaccess = $mem->get("maxaccess");
$this->maxaccesstime = $mem->get("maxaccesstime");
$this->lastday = $mem->get("lastday");
$this->tempfile = TEMPFILE;
return;
}
//如果在memcache中读取的数据为空,则从数据库中读取
//初始化数据库表格
$this->onlinefiletname = ONLINETNAME;
$this->ipaccesstname = ACCESSTNAME;
$this->staticstname = STATICSTNAME;
$this->link =mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_DATA) or die("Could not connect to MySQL server!");
$sql = "SHOW COLUMNS FROM $this->ipaccesstname";
$result = mysqli_query($this->link,$sql);
if(!$result||mysqli_num_rows($result)<=0)
{
$sql = "CREATE TABLE IF NOT EXISTS `$this->ipaccesstname` (
`totalaccess` bigint(20) NOT NULL,
`todayaccess` bigint(20) NOT NULL,
`yestodayaccess` bigint(20) NOT NULL,
`time` timestamp NOT NULL,
`maxaccess` bigint(20) NOT NULL,
`maxaccesstime` timestamp NOT NULL,
`other` bigint(20) NOT NULL,
PRIMARY KEY (`totalaccess`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
mysqli_query($this->link,$sql);
}
$sql = "SHOW COLUMNS FROM $this->staticstname";
$result = mysqli_query($this->link,$sql);
if(!$result||mysqli_num_rows($result)<=0)
{
$sql = "CREATE TABLE IF NOT EXISTS `$this->staticstname` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`time` timestamp NOT NULL,
`staytime` bigint(20) NOT NULL,
`ip` VARCHAR(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
mysqli_query($this->link,$sql);
}
$sql = "SHOW COLUMNS FROM $this->onlinefiletname";
$result = mysqli_query($this->link,$sql);
if(!$result||mysqli_num_rows($result)<=0)
{
$sql = "CREATE TABLE IF NOT EXISTS `$this->onlinefiletname` (
`ip` VARCHAR(100) NOT NULL,
`time` bigint(20) NOT NULL,
`other` VARCHAR(64),
PRIMARY KEY (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
mysqli_query($this->link,$sql);
}
$this->t_now = time();
$this->t_array = getdate($this->t_now);
$this->day = $this->t_array['mday'];
$this->mon = $this->t_array['mon'];
$this->year = $this->t_array['year'];
$this->total_c=0;
$this->today_c=0;
$this->yesterday_c=0;
$this->your_c=0;
$this->total_online=0;
$this->startno = STARTUP;
$this->maxaccess = 0;
$this->maxaccesstime =0;
$this->tempfile = TEMPFILE;
}
//刷新更新数据
public function Refresh()
{
//如果memcache中有记录,则所有的值从memcache中读取,这步骤可省略,初始化已经有了
$this->total_c = $this->mem->get("total_c");
if(!empty($this->total_c)&&$this->total_c>0)
{
$this->today_c=$this->mem->get("today_c");
$this->yesterday_c=$this->mem->get("yesterday_c");
$this->your_c=$this->mem->get("your_c");
$this->total_online=$this->mem->get("total_online");
$this->startno = $this->mem->get("startno");
$this->maxaccess = $this->mem->get("maxaccess");
$this->maxaccesstime = $this->mem->get("maxaccesstime");
$this->lastday = $this->mem->get("lastday");
if ( !isset($HTTP_COOKIE_VARS["countcookie"]) ||
$HTTP_COOKIE_VARS["countcookie"] != $this->day)
{
$this->your_c=1;
$lockfile=fopen("$this->tempfile","a");
flock($lockfile,3);
putenv('TZ=JST-9');
$t_array2 = getdate($this->t_now-24*3600);
$day2=$t_array2['mday'];
$mon2=$t_array2['mon'];
$year2=$t_array2['year'];
$mon = ($this->mon>10)?$this->mon:"0".$this->mon;
$da = ($this->day>10)?$this->day:"0".$this->day;
$day2= ($day2>10)?$day2:"0".$day2;
$mon2= ($mon2>10)?$mon2:"0".$mon2;
$today = $this->year."-".$mon."-".$da;
$yesterday = "$year2-$mon2-$day2";
//更新今日访问量和总访问量
if (trim($today) != trim($this->lastday)) {
if ($yesterday != $this->lastday)
$this->yesterday_c = 0;
else
$this->yesterday_c = $this->today_c;
$this->today_c = 0;
$this->lastday = $today;
}
$this->total_c++;
$this->today_c++;
//格式化输出
$this->total_c = sprintf("%06d", $this->total_c);
$this->today_c = sprintf("%06d", $this->today_c);
$this->yesterday_c = sprintf("%06d", $this->yesterday_c);
setcookie("countcookie","$this->day",$this->t_now+432