流量統計功能
顯示效果:
總訪問量:399
今日流量:14
昨日流量:16
本代碼僅供學習交流,其中必有不妥之處。請見諒!
--
-- 表的結構 `mycounter`
--
復制代碼 代碼如下:
CREATE TABLE `mycounter` (
`id` int(11) NOT NULL auto_increment,
`Counter` int(11) NOT NULL,
`CounterLastDay` int(10) default NULL,
`CounterToday` int(10) default NULL,
`RecordDate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=2 ;
函數過程如下:
復制代碼 代碼如下:
<?PHP
public function ShowMyCounter(){
//定義變量
$IsGone = FALSE;
//讀取數據
$querysql = "SELECT * FROM `mycounter` WHERE id = Ƈ' ";
$queryset = mysql_query($querysql);
$row = mysql_fetch_array($queryset);
//獲得時間量
$DateNow = date('Y-m-d');
$RecordDate = $row['RecordDate'];
$DateNow_explode = explode("-",$DateNow);
$RecordDate_explode = explode("-",$RecordDate);
//判斷是否已過去一天
if( $DateNow_explode[0] > $RecordDate_explode[0]) $IsGone = TRUE;
else if( $DateNow_explode[0] == $RecordDate_explode[0] ){
if( $DateNow_explode[1] > $RecordDate_explode[1] ) $IsGone = TRUE;
else if( $DateNow_explode[1] == $RecordDate_explode[1] ){
if( $DateNow_explode[2] > $RecordDate_explode[2] ) $IsGone = TRUE;
}else BREAK;
}else BREAK;
//根據IsGone進行相應操作
IF($IsGone) {
$RecordDate = $DateNow;
$CounterToday = 0;
$CounterLastDay = $row['CounterToday'];
$upd_sql = "update mycounter set RecordDate = '$RecordDate',CounterToday = '$CounterToday',CounterLastDay = '$CounterLastDay' WHERE id = Ƈ' ";
mysql_query($upd_sql);
}
//再次獲取數據
$querysql = "SELECT * FROM `mycounter` WHERE id = Ƈ' ";
$queryset = mysql_query($querysql);
$Counter = $row['Counter'];
$CounterToday = $row['CounterToday'];
$CounterLastDay = $row['CounterLastDay'];
if($row = mysql_fetch_array($queryset) ){
if( $_COOKIE["user"] != "oldGuest" ){
$Counter = ++$row['Counter'];
$CounterToday = ++$row['CounterToday'];
$upd_sql = "update mycounter set counter = '$Counter',CounterToday = '$CounterToday' WHERE id = Ƈ' ";
$myquery = mysql_query($upd_sql);
}
echo "總訪問量:".$Counter;
echo "
";
echo "今日流量:".$CounterToday;
echo "
";
echo "昨日流量:".$CounterLastDay;
}else{//如果數據庫為空時,相應的操作
}
}
?>
當然,需要在文件第一行開始寫出如下代碼:
復制代碼 代碼如下:
<?PHP
session_start();
if( !isset($_COOKIE["user"]) ){
setcookie("user","newGuest",time()+3600);
}else {
setcookie("user","oldGuest");
}
?>