訪客計數器
<?php
class visitData
{
var $userName = "root"; //帳號
var $password = "12345"; //密碼
var $hostName = "localhost"; //主機(Server)名稱
//設定目前頁面訪問數
function setDailyPageNum($setDate)
{
//建立資料連結
$mylink = mysql_connect($this-> hostName, $this-> userName, $this-> password);
//選擇資料庫
mysql_select_db("visitdata", $mylink);
//指定提取資料的校對字元表
mysql_query("set character set big5");
$sql="select * from dailyistics where visitDate='" . $setDate . "'";
$dataList=mysql_query($sql);
if ($arr_result=mysql_fetch_array($dataList,MYSQL_BOTH))
{
$sql="update dailyistics set pageNum=pageNum+1 where visitDate='" . $setDate . "'";
mysql_query($sql);
}else{
$sql="insert into dailyistics (pageNum,visitNum,visitDate) value(1,0,'" . $setDate . "')";
mysql_query($sql);
}
session_start();
if (! session_is_registered("userIP")){
session_register("userIP");
$_SESSION["userIP"]=$this->getIP;
$sql="update dailyistics set visitNum=visitNum+1 where visitDate='" . $setDate . "'";
mysql_query($sql);
}else{
$_SESSION["userIP"]=$this->getIP;
}
}
//取得頁面總訪問數
function getDailyTotalPageNum(){
//建立資料連結
$mylink = mysql_connect($this-> hostName, $this-> userName, $this-> password);
//選擇資料庫
mysql_select_db("visitdata", $mylink);
//指定提取資料的校對字元表
mysql_query("set character set big5");
$sql="select sum(pageNum) as v_total from dailyistics";
$dataList=mysql_query($sql);
$arr_result=mysql_fetch_array($dataList,MYSQL_BOTH);
$v_total=$arr_result['v_total'];
return ($v_total>0)?$v_total:0;
}
//取得特定日期的頁面訪問數
function getDailyPageNum($setDate){
//建立資料連結
$mylink = mysql_connect($this-> hostName, $this-> userName, $this-> password);
//選擇資料庫
mysql_select_db("visitdata", $mylink);
//指定提取資料的校對字元表
mysql_query("set character set big5");
$sql="select pageNum from dailyistics where visitDate='" . $setDate . "'";
$dataList=mysql_query($sql);
$arr_result=mysql_fetch_array($dataList,MYSQL_BOTH);
$pageNum=$arr_result['pageNum'];
return ($pageNum>0)?$pageNum:0;
}
//取得訪客IP
function getIP() {
global $_SERVER;
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
$onlineip = preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
return $onlineipmatches[0];
}
//取得總訪客數量
function getDailyTotalVisitNum(){
//建立資料連結
$mylink = mysql_connect($this-> hostName, $this-> userName, $this-> password);
//選擇資料庫
mysql_select_db("visitdata", $mylink);
//指定提取資料的校對字元表
mysql_query("set character set big5");
$sql="select sum(visitNum) as v_total from dailyistics";
$dataList=mysql_query($sql);
$arr_result=mysql_fetch_array($dataList,MYSQL_BOTH);
$v_total=$arr_result['v_total'];
return ($v_total>0)?$v_total:0;
}
//取得特定日期訪客數量
function getDailyVisitNum($setDate){
//建立資料連結
$mylink = mysql_connect($this-> hostName, $this-> userName, $this-> password);
//選擇資料庫
mysql_select_db("visitdata", $mylink);
//指定提取資料的校對字元表
mysql_query("set character set big5");
$sql="select visitNum from dailyistics where visitDate='" . $setDate . "'";
$dataList=mysql_query($sql);
$arr_result=mysql_fetch_array($dataList,MYSQL_BOTH);
$visitNum=$arr_result['visitNum'];
return ($visitNum>0)?$visitNum:0;
}
}
?>
留言
張貼留言