php統(tǒng)計網(wǎng)站/html頁面瀏覽訪問次數(shù)程序
本文章來給大這介紹了php自己寫的一些常用的網(wǎng)站統(tǒng)計代碼寫法,用無數(shù)據(jù)庫的與使用數(shù)據(jù)庫及html靜態(tài)頁面瀏覽資次數(shù)統(tǒng)計代碼,大家可進入?yún)⒖肌?div style="height:15px;">
實例1
直接使用txt文件進行統(tǒng)計的代碼
代碼如下復制代碼
fwrite($fp,0);
fclose($fp);
counter($filepath);
}else
{
counter($filepath);
}
$_SESSION['temp'] = 1;//登錄以后,給$_SESSION[temp]賦一個值1
}
echo '歡迎來到懶人站長素材網(wǎng)站,您是本站第'.file_get_contents($filepath).'位訪客';
//counter()方法用來得到文件內的數(shù)字
function counter($f_value)
{
//用w模式打開文件時會清空里面的內容,所以先用r模式打開,取出文件內容,保存到變量
$fp = fopen($f_value,'r') or die('打開文件時出錯。');
$countNum = fgets($fp,1024);
fclose($fp);
$countNum++;
$fpw = fopen($f_value,'w');
fwrite($fpw,$countNum);
fclose($fpw);
}
//注釋下面一行可以實現(xiàn)同一IP登錄不累加效果,測試時可以打開
session_destroy();
>
上面使用的是txt文件,下面我們來介紹一個mysql
數(shù)據(jù)庫操作實例
代碼如下復制代碼
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 ;
函數(shù)
代碼如下復制代碼
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;
//根據(jù)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);
}
//再次獲取數(shù)據(jù)
$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{//如果
數(shù)據(jù)庫為空時,相應的操作
}
}
>
當然,需要在文件第一行開始寫出如下代碼:
代碼如下復制代碼
如果是靜態(tài)頁面我們上面的方法是不可以實現(xiàn)的,但下面再舉一個不錯的統(tǒng)計實例
代碼如下復制代碼
Insert title here
javascript' src='count.php?aid=1&t=show' mce_src='count.php?aid=1&t=show'>
php統(tǒng)計靜態(tài)html頁面瀏覽訪問次數(shù)代碼
count.php代碼
代碼如下復制代碼
數(shù)據(jù)庫
代碼如下復制代碼
--
-- 表的結構 `count`
--
CREATE TABLE IF NOT EXISTS `count` (
`id` int(11) NOT NULL auto_increment,
`aid` int(11) default NULL,
`click_num` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=2 ;