我們給大家一個很實用訪問量統計類,希望大家能夠在工作和學習中使用。
<?php
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SQL:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DROP TABLE IF EXISTS hl_counter;
CREATE TABLE `hl_counter` (
`id` int(11) NOT NULL auto_increment,
`ip` varchar(50) NOT NULL COMMENT 'IP地址',
`counts` varchar(50) NOT NULL COMMENT '統計訪問次數',
`date` datetime NOT NULL COMMENT '訪問時間',
PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=gb2312;
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/**
+----------------------------------------------------------------------
使用實例:
+----------------------------------------------------------------------
$counts_visits = new counter('hl_counter'); 實例化對象
+----------------------------------------------------------------------
記錄訪問數:
$counts_visits->record_visits();
+----------------------------------------------------------------------
獲取訪問數據:
$counts_visits->get_sum_visits(); 獲取總訪問量
$counts_visits->get_sum_ip_visits(); 獲取總IP訪問量
$counts_visits->get_month_visits(); 獲取當月訪問量
$counts_visits->get_month_ip_visits(); 獲取當月IP訪問量
$counts_visits->get_date_visits(); 獲取當日訪問量
$counts_visits->get_date_ip_visits(); 獲取當日IP訪問量
+----------------------------------------------------------------------
上述僅為邏輯演示,本類可靈活使用
+----------------------------------------------------------------------
*/
class counts_visits{
/*
* 獲取表名
*
* @private String
*/
private $table;
/**
* 構造函數
*
* @Access public
* @parameter string $table 表名
* @return void
*/
public function __construct($table){
$this->table = $table;
}
/**
* 獲得客戶端真實的IP地址
*
* @Access public
* @return void
*/
public function getip(){
if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){
$ip = getenv("HTTP_CLIENT_IP");
}else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
$ip = getenv("HTTP_X_FORWARDED_FOR");
}else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){
$ip = getenv("REMOTE_ADDR");
}else if(isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
$ip = $_SERVER['REMOTE_ADDR'];
}else{
$ip = "unknown";
}
return ($ip);
}
/**
* 記錄訪問數(默認一個IP每天只統計一次)
*
* @Access public
* @return void
*/
public function record_visits(){
$ip = $this->getip(); //獲得客戶端真實的IP地址
$result = MySQL_query("select * from $this->table where ip = '$ip'");
$row = MySQL_fetch_array($result);
if(is_array($row)){
if(!$_COOKIE['visits']){
MySQL_query("UPDATE $this->table SET `counts` = '".($row[counts]+1)."' WHERE `ip` = '$ip' LIMIT 1 ;");
}
}else{
MySQL_query("INSERT INTO $this->table(`id`,`ip`,`counts`,`date`)VALUES (NULL,'$ip','1',Now());");
setcookIE('visits',$ip,time()+3600*24);
}
}
/*
* 獲取總訪問量、月訪問量、日訪問量的共有方法
*
* @Access private
* @parameter string $condition sql語句條件
* @return integer
*/
private function get_visits($condition = ''){
if($condition == ''){
$query = MySQL_query("select sum(counts) as counts from $this->table");
}else{
$query = MySQL_query("select sum(counts) as counts from $this->table where $condition");
}
return MySQL_result($query,0,'counts');
}
/*
* 獲取IP訪問量的共有方法
*
* @Access private
* @parameter string $condition sql語句條件
* @return integer
*/
private function get_ip_visits($condition = ''){
if($condition == ''){
$query = MySQL_query("select * from $this->table");
}else{
$query = MySQL_query("select * from $this->table where $condition");
}
while($row = MySQL_fetch_array($query)){
$ip_visits_arr[] = $row['ip'];
}
$ip_visits = count($ip_visits_arr);
return $ip_visits;
}
/**
* 獲取總訪問量
*
* @Access public
* @return integer
*/
public function get_sum_visits(){
return $this->get_visits();
}
/**
* 獲取總IP訪問量
*
* @Access public
* @return integer
*/
public function get_sum_ip_visits(){
return $this->get_ip_visits();
}
/**
* 獲取當月訪問量
*
* @Access public
* @return integer
*/
public function get_month_visits(){
return $this->get_visits("DATE_FORMAT(date,'%Y-%m') = '".substr(date('Y-m-d'),0,7)."'");
}
/**
* 獲取當月IP訪問量
*
* @Access public
* @return integer
*/
public function get_month_ip_visits(){
return $this->get_ip_visits("DATE_FORMAT(date,'%Y-%m') = '".substr(date('Y-m-d'),0,7)."'");
}
/**
* 獲取當日訪問量
*
* @Access public
* @return integer
*/
public function get_date_visits(){
return $this->get_visits("DATE_FORMAT(date,'%Y-%m-%d') = '".date('Y-m-d')."'");
}
/**
* 獲取當日IP訪問量
*
* @Access public
* @return integer
*/
public function get_date_ip_visits(){
return $this->get_ip_visits("DATE_FORMAT(date,'%Y-%m-%d') = '".date('Y-m-d')."'");
}
}
?>