php统计网站/html页面浏览访问次数程序
本文章来给大这介绍了php自己写的一些常用的网站统计代码写法,用无数据库的与使用数据库及html静态页面浏览资次数统计代码,大家可进入参考。
实例1
直接使用txt文件进行统计的代码
代码如下
<?php
session_start();//定义session,同一IP登录不累加
$filepath = '';
if ($_SESSION['temp'] == '')//判断$_SESSION[temp]的值是否为空,其中的temp为自定义的变量
{
if (!file_exists($filepath))//检查文件是否存在,不存在刚新建该文件并赋值为0
session_start();//定义session,同一IP登录不累加
$filepath = '';
if ($_SESSION['temp'] == '')//判断$_SESSION[temp]的值是否为空,其中的temp为自定义的变量
{
if (!file_exists($filepath))//检查文件是否存在,不存在刚新建该文件并赋值为0
{
$fp = fopen($filepath,'w');
fwrite($fp,0);
fclose($fp);
counter($filepath);
}else
{
counter($filepath);
}
$_SESSION['temp'] = 1;//登录以后,给$_SESSION[temp]赋一个值1
}
//counter()方法用来得到文件内的数字
$fp = fopen($filepath,'w');
fwrite($fp,0);
fclose($fp);
counter($filepath);
}else
{
counter($filepath);
}
$_SESSION['temp'] = 1;//登录以后,给$_SESSION[temp]赋一个值1
}
//counter()方法用来得到文件内的数字
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);
}
//注释下面一行可以实现同一IP登录不累加效果,测试时可以打开
session_destroy();
?>
$fp = fopen($f_value,'r') or die('打开文件时出错。');
$countNum = fgets($fp,1024);
fclose($fp);
$countNum++;
$fpw = fopen($f_value,'w');
fwrite($fpw,$countNum);
fclose($fpw);
}
//注释下面一行可以实现同一IP登录不累加效果,测试时可以打开
session_destroy();
?>
上面使用的是txt文件,下面我们来介绍一个常用的php代码实例mysql数据库操作实例
代码如下
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 ;
`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;
/
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;
$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'];
}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 "
$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{//如果数据库为空时,相应的操作
}
}
?>
echo "昨日流量:".$CounterLastDay;
}else{//如果数据库为空时,相应的操作
}
}
?>
当然,需要在文件第一行开始写出如下代码:
代码如下
<?PHP
session_start();
if( !isset($_COOKIE["user"]) ){
setcookie("user","newGuest",time()+3600);
}else {
setcookie("user","oldGuest");
session_start();
if( !isset($_COOKIE["user"]) ){
setcookie("user","newGuest",time()+3600);
}else {
setcookie("user","oldGuest");
}
?>
?>
如果是静态页面我们上面的方法是不可以实现的,但下面再举一个不错的统计实例
代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<mce:script language="javascript" src="count.php?aid=1&t=show" mce_src="count.php?aid=1&t=show"></mce:script>
<mce:script language="javascript" src="count.php?aid=1" mce_src="count.php?aid=1"></mce:script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<mce:script language="javascript" src="count.php?aid=1&t=show" mce_src="count.php?aid=1&t=show"></mce:script>
<mce:script language="javascript" src="count.php?aid=1" mce_src="count.php?aid=1"></mce:script>
</head>
<body>
<h1>php统计静态html页面浏览访问次数代码</h1>
<hr>
</body>
</html>
<body>
<h1>php统计静态html页面浏览访问次数代码</h1>
<hr>
</body>
</html>
count.php代码
代码如下
<?php
$aid = isset( $_GET['aid'] )?$_GET['aid']:'';
$t = isset( $_GET['t'] )?$_GET['t']:'';
if(intval( $aid )){
if( $t =='show' ){
echo "document.write('这里是显示浏览次数,可以从数据库读出来');";
$aid = isset( $_GET['aid'] )?$_GET['aid']:'';
$t = isset( $_GET['t'] )?$_GET['t']:'';
if(intval( $aid )){
if( $t =='show' ){
echo "document.write('这里是显示浏览次数,可以从数据库读出来');";
}
else{
$conn = mysql_connect('localhost','root','root') ;
$sql = "Update count set click_num = click_num+1 where aid ='$aid'";
mysql_db_query('db_test',$sql,$conn);
}
}
?>
else{
$conn = mysql_connect('localhost','root','root') ;
$sql = "Update count set click_num = click_num+1 where aid ='$aid'";
mysql_db_query('db_test',$sql,$conn);
}
}
?>
数据库
代码如下
--
-- 表的结构 `count`
--
CREATE TABLE IF NOT EXISTS `count` (
-- 表的结构 `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 ;
`aid` int(11) default NULL,
`click_num` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=2 ;
文档资料参照:一览(www.yl1001)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论