本文章来给大这介绍了php自己写的一些常用的网站统计代码写法,用无数据库的与使用数据库及html静态页面浏览资次数统计代码,大家可进入参考。
实例1
直接使用txt文件进行统计的代码
代码如下 | 复制代码 |
session_start();//定义session,同一IP登录不累加 function counter($f_value) |
上面使用的是txt文件,下面我们来介绍一个mysql数据库操作实例
代码如下 | 复制代码 |
CREATE TABLE `mycounter` ( |
函数
代码如下 | 复制代码 |
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{//如果数据库为空时,相应的操作 } } ?> |
当然,需要在文件第一行开始写出如下代码:
代码如下 | 复制代码 |
session_start(); |
如果是静态页面我们上面的方法是不可以实现的,但下面再举一个不错的统计实例
代码如下 | 复制代码 |
php统计静态html页面浏览访问次数代码 |
count.php代码
代码如下 | 复制代码 |
$aid = isset( $_GET['aid'] )?$_GET['aid']:''; |
数据库
代码如下 | 复制代码 |
-- |