访问量统计代码
public class Counter {
private int count;
// 每访问一次,计数器自加一
public int getCount() {
return ++count;
}
public void setCount(int count) {
this.count = count;
}
}
<%-- 定义一个 session 范围内的计数器 记录个人访问信息 --%>
<jsp:useBean id="personCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="session" />
<%-- 定义一个 application 范围内的计数器 记录所有人的访问信息 --%>
<jsp:useBean id="totalCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="application" />
<div align="center">
<form action="method.jsp" method="get">
<fieldset style='width: 300'>
<legend>计数器</legend>
<table align="center" width="400">
<tr>
<td width=150 align="right" style="font-weight:bold; ">您的访问次数:</td>
<td>
<%-- 获取个人的 访问次数 --%>
<jsp:getProperty name="personCount" property="count" /> 次
</td>
</tr>
<tr>
<td width=150 align="right" style="font-weight:bold; ">总共的访问次数:</td>
<td>
<%-- 获取所有人的 访问次数 --%>
<jsp:getProperty name="totalCount" property="count" /> 次
</td>
</tr>
</table>
</fieldset>
</form>
</div>
希望你能帮到你
2. html的统计访客人数的代码
静态页面的程序本身是不能调用数据库来实现当前页面访问量统计的,包括实现被访问次数、访问次数增加等功能。但是静态页面如果没有这么一个功能,却又总觉得比动态页面少了些什么。
通过js后台ajax请求修改访问数。
基于jquery:
<script>
$(function() {
$.get('update.php?id=1',{r:Math.random()});
//当然$.post()、$.ajax()等都可以咯。
//然后要记得加一个随机数,因为如果不加的话,有的浏览器会认为是同一个请求,然后不请求。
});
</script>
这样写:
<script src="update.php?id=1"></script>
经测试,这样也是可行的。
至于文章的id,在静态化的过程中,可以直接赋值到页面要请求的网址参数后。
update.php的话,就是连接数据库,通过传过来的文章id,更新访问量的处理咯。
如果要实现在静态页面马上显示更新的数目的话:
方法一需要在ajax请求后调用回调函数,然后update.php返回新的访问量,然后在回调函数中定位到显示访问量位置,替换成新的访问量。
代码实现:
$(function() {
$.get('update.php?id=1',{r:Math.random()},function(num) {
$('#hit').html(num);
});
});
方法二则需要在update.php中,添加一句话:
document.write(<?php echo $num; ?>);
3. 统计当前网页被访问次数的Jquery代码:
最简单的办法:
<scripttype="text/javascript">
if(localStorage.pagecount){localStorage.pagecount=Number(localStorage.pagecount)+1;}else{localStorage.pagecount=1;}document.write("访问数:"+localStorage.pagecount+"time(s).");</script>4. php流量统计功能的实现代码
流量统计功能
显示效果:
总访问量: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");
}
?>
5. PHP网站怎么加网站访问量统计
1、本地新建一个空白文档,命名为cnt.php
2、用记事本打开cnt.php,然后将代码复制到cnt.php里面。代码如下:
<?php
$n=file_get_contents('cnt.txt');
$n++;
file_put_contents('cnt.txt',$n);
echo "document.write($n);";
?>
3、保存cnt.php文件,并上传到服务器。
4、然后在主页或者其它页面里面这样调用计数器:
”你是第<script type=text/javascript src=cnt.php></script>位访问者“
就可以了。
5、这样就可以清楚的知道了有多少访客来访了。这个除了在首页可以使用外,其他页面也可以通过这个方法进行调用,只需要在你需要统计的地方添加上这个”你是第<script type=text/javascript src=cnt.php></script>位访问者“就可以轻松知道你需要统计的地方有多少访客了。
6. java如何统计网站访问量
步骤一、建一个表,表名任意,这里取名为:visitorcounter,表的结构如下所示:
+-------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| vdate | date | NO | | 2000-01-01 | |
| vnum | int(11) | NO | | 0 | |
+-------+------------------+------+-----+------------+----------------+
步骤二、建立一个java类,名字也为:visitorcounter,类的内容如下:
package com.hdzx.pub;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
public class VisitorCounter {
private final static String TABLE_NAME = "visitorcounter";
private static String today = null;
private static long today_num = 0;
private static long total_num = 0;
//加载访问量
public static void loadNum(){
if(total_num<1)
loadTotalNum();
if(today_num<1)
loadToadyNum();
}
//加载今日访问量
private static void loadToadyNum() {
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where vdate='"+today+"'";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
today_num = rs.getLong("vnum");
}
else
{
sql = "insert into "+TABLE_NAME+"(vdate,vnum) values('"+today+"',0)";
db.executeUpdate(sql);
today_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
today_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
}
}
//加载总访问量
private static void loadTotalNum() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where id=1";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
total_num = rs.getLong("vnum");
}
else
{
total_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
total_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
}
}
//增加总的访问量
private static int incTotalCounter(){
int k = 0;
DBConnect db = null;
loadNum();
total_num = total_num+1;
String sql = "update "+TABLE_NAME+" set vnum="+total_num+" where id=1";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
}
return k;
}
//增加今日的访问量
public static int incTodayCounter(){
int k = 0;
DBConnect db = null;
String sql = null;
loadNum();
today_num += 1;
sql = "update "+TABLE_NAME+" set vnum="+today_num+" where vdate='"+today+"'";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
if(k > 0)
incTotalCounter();
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
}
return k;
}
//获得今天的日期
private static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
///获得今日访问量
public static long getTodayNum(){
loadNum();
return today_num;
}
//获得总的访问量
public static long getTotalNum(){
loadNum();
return total_num;
}
}
步骤三、经过以上的步骤后,在页面中加入以下的代码,就可以实现网站访问量的统计工作:
if(session.isNew())
{
VisitorCounter.incTodayCounter();
}
%>
今日访问量:<%=VisitorCounter.getTodayNum() %><br/>
总的访问量: <%=VisitorCounter.getTotalNum() %>
7. 如何使用python 统计网站访问量并生成报表
统计网站访问量
统计出每个IP的访问量有多少?(从日志文件中查找)
#!/usr/bin/env python
#!coding=utf-8
list = []
f = file('/tmp/1.log')
str1 = f.readlines()
f.close()
for i in str1:
ip = i.split()[0] //split()通过指定分隔符对字符串进行切片,默认为所有的空字符;split分隔后是一个列表,[0]表示取其第一个元素;
list.append(ip)//追加
list_num = set(list)
for j in list_num:
num = list.count(j)
print '%s : %s' %(j,num)
生成报表
#_*_coding:utf-8_*_
import MySQLdb
import xlwt
from datetime import datetime
def get_data(sql):
# 创建数据库连接.
conn = MySQLdb.connect(host='127.0.0.1',user='root'\
,passwd='123456',db='test',port=3306,charset='utf8')
# 创建游标
cur = conn.cursor()
# 执行查询,
cur.execute(sql)
# 由于查询语句仅会返回受影响的记录条数并不会返回数据库中实际的值,所以此处需要fetchall()来获取所有内容。
result = cur.fetchall()
#关闭游标
cur.close()
#关闭数据库连接
conn.close
# 返给结果给函数调用者。
return result
def write_data_to_excel(name,sql):
# 将sql作为参数传递调用get_data并将结果赋值给result,(result为一个嵌套元组)
result = get_data(sql)
# 实例化一个Workbook()对象(即excel文件)
wbk = xlwt.Workbook()
# 新建一个名为Sheet1的excel sheet。此处的cell_overwrite_ok =True是为了能对同一个单元格重复操作。
sheet = wbk.add_sheet('Sheet1',cell_overwrite_ok=True)
# 获取当前日期,得到一个datetime对象如:(2016, 8, 9, 23, 12, 23, 424000)
today = datetime.today()
# 将获取到的datetime对象仅取日期如:2016-8-9
today_date = datetime.date(today)
# 遍历result中的没个元素。
for i in xrange(len(result)):
#对result的每个子元素作遍历,
for j in xrange(len(result[i])):
#将每一行的每个元素按行号i,列号j,写入到excel中。
sheet.write(i,j,result[i][j])
# 以传递的name+当前日期作为excel名称保存。
wbk.save(name+str(today_date)+'.xls')
# 如果该文件不是被import,则执行下面代码。
if __name__ == '__main__':
#定义一个字典,key为对应的数据类型也用作excel命名,value为查询语句
db_dict = {'test':'select * from student'}
# 遍历字典每个元素的key和value。
for k,v in db_dict.items():
# 用字典的每个key和value调用write_data_to_excel函数。
write_data_to_excel(k,v)
8. 有统计网站访问量的代码吗
假定数据存在 abc.mdb中
abc.mdb中字段如下:
序号(自动)
日期(访客进入时间)
电脑(IP地址)
来自(如果访客从www.0086it.com/?f=hello 进入本站,那会显示“hello”)
地址(通过对IP地址分析后知道的地址(如:中国网通或北京大学))
在网站首页中插入以下代码:
《%
if session("0086it")<>1 then
'上面一行防止刷新给统计造成不准。
dsntemp=server.mappath("abc.mdb")
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&dsntemp
set rs=server.createobject("adodb.recordset")
rs.open "db",conn,1,3
rs.addnew
rs("日期")=now()
rs("电脑")=request.servervariables("remote_addr")
rs("来自")=request.querystring("f")&"◆"&request.serverVariables("Http_REFERER")
rs("地址")=session("laizi")
'session("laizi")的值的取得不作具体介绍,是由另一程序将访者ip地址与另外一个数据库对比中得出来“详细汉字地名,相当于IP地址查询软件中的功能)
rs.update
rs.close
set rs=nothing
conn.close
set conn=nothing
session("0086it")=1
end if
%》
这样,每次访客访问我站,就可以记录他的信息。
当然,我还需要有一个程序来读后台。
程序如下:
《%
'**********************************
'
' 访 客 统 计 系 统'
'
' 程序设计 : 姜川
' [email protected]
' COPY请保留以上信息
'
'*********************************
'
response.expires=0
Response.Buffer=True
dim id
id=request.querystring("id")
if id="" then
id=50
end if
%》
《html》
《style type="text/css"》
《link rel="stylesheet" href="../css/one.css" type="text/css"》
《!--
.jiangc { font-size: 9pt; line-height: 12pt}
a { color: #FF0000; text-decoration: none}
a:hover { text-decoration: underline}
--》
《/style》
《body bgcolor="#FFFFFF"》
《%
dsntemp=server.mappath("abc.mdb")
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};uid=admin;password=hello;dbq="&dsntemp
if request.querystring("cha")《》"" then
sql ="select * from db where 来自 like '%"&request.form("cha")&"%' order by 日期 DESC"
else
sql ="select * from db order by 序号 DESC"
end if
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
%》
《p align="center"》《br》
《font face="黑体"》访 问 统 计 系 统《/font》《/p》
《table width="700" border="0" cellspacing="1" cellpadding="0" align="center" class="jiangc" bgcolor="#000000"》
《form name="form1" method="post" action="?cha=1"》 《tr》
《td height="24" bgcolor="#ECF9FF" align="center"》 [ 共 《font color=red》《%=rs.recordcount%》《/font》
条记录 ] 列出最近 《a href="?id=100"》100《/a》 《a href="?id=300"》300《/a》 《a href="?id=500"》500《/a》
《a href="?id=1000"》1000《/a》 《a href="?id=3000"》3000《/a》 《a href="?id=5000"》5000《/a》
《a href="?ID=《%=rs.recordcount%》&ID2=all"》所有《/a》 记录
《input type="text" name="cha" class="jiangc" size="12"》
《input type="submit" name="Submit" value="查" class="jiangc"》
《/td》
《/tr》 《/form》
《/table》
《table width="100%" border="0" cellspacing="0"》
《tr》
《td height=2》《/td》
《/tr》
《/table》
《table border="0" cellspacing="1" cellpadding="2" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF" class="jiangc" align="center" bgcolor="#999999"》
《tr bgcolor="#CCCCCC"》
《td》 序号《/td》
《td》记录中总编号《/td》
《td》访问者进入日期《br》
0000000000000000000《/td》
《td》 访问者电脑IP地址《/td》
《td》 地区《/td》
《td》 来自《/td》
《/tr》
《%
while not rs.eof and i《 cint(id)
i=i+1
%》
《tr bgcolor="#FFFFFF"》
《td align="center"》《font color=cccccc》《%=i%》《/font》《/td》
《td align="center"》 《%=rs("序号")%》 《/td》
《td》
《%
if rs("日期") 》 date() then
response.write "《font color=red》"&rs("日期")&"《/font》"
else
response.write rs("日期")
end if%》
《/td》
《td》
《%if rs("电脑")="221.215.99.61" then response.write "*" else response.write rs("电脑") end if%》
《/td》
《td》
《%=rs("地址")%》
《/td》
《td》
《%if instr(rs("来自"),"◆")《》0 then
response.write "《a href='"&right(rs("来自"),len(rs("来自"))-instr(rs("来自"),"◆"))&"' target='_blank'》"&rs("来自")&"《/a》"
end if%》
《/td》
《/tr》
《%
rs.movenext
wend
%》
《/table》
《br》
《table width="700" border="0" cellspacing="1" cellpadding="10" align="center" class="jiangc" bgcolor="#CCCCCC" bordercolor="#0000CC"》
《tr》
《td bgcolor="#EFEFEF"》备 注:《%if request.querystring("id2")=all then%》只列出最近的 《font color=red》《%=id%》《/font》 条记录《br》
《%else%》
系统列出了所有访问记录《br》
《%end if%》
设 计:[email protected](MSN)《br》
设计日期:2003年03月《/td》
《/tr》
《/table》
《/html》