技术文栏 - 软件专区 - 设计开发 - 浏览文章 - 入门训练:写程序应注意的事项1
入门训练:写程序应注意的事项1
http://www.jisuyu.cn 2007-1-10 9:56:21
最近用ASP做了一个小论坛,客户又要求要统计在线人数,所以花了点时间写一个在线统计访会员的功能。写的过程中也查看了许多文档。自我感觉用的方法能比较准确的统计在线人数。当然一定也有不足的地方,希望各位老师纠正。

特别说明:

  本论坛登录是采用用户名登录,登录后取得用户昵称。整个网站不显示用户名,我想相对来说安全一点吧,所以有昵称和用户名区别。

  1。建立数据库表
  表名为online
  设如下字段
  id '用来记录每一个访问都的session.sessionid
  name '如果是访客,则记录为访客。
  online  '如果是访各为0 如果是会员1
  datetime '最近活动时间
  username '会员的登录用户名,访客为空。
  ip '记录访问都的登录IP

  head.asp '向数据库表写入在线人数,该页面必须搜入到每一个用于浏览ASP页面中。


<%
set rs=Server.CreateObject("ADODB.Recordset")
if session("username")="" then 判断用户未登录
sql="select * from online where id='"&session.sessionid&"' "  '判断这个sessionid是否存在于数据库表中.
rs.Open sql,Conn,1,3
if rs.eof then  '访客第一次浏览
rs.addnew
rs("id")=session.sessionID
rs("name")="游客"
rs("online")=0      '0表示用户未登陆,是游客身份
rs("datetime")=now()
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If userip = "" Then
        userip= Request.ServerVariables("REMOTE_ADDR")
        end if
  rs("ip")=userip
else      '访客非第一次浏览





rs("datetime")=now()  '更新活动时间
rs.update
rs.close
end if
else
sql="select * from online where id='" & session.sessionID & "' or admin='"&session("username")&"'"  '判断sessionid 或者 用户名记录已存在数据表中。
rs.Open sql,Conn,1,3
if rs.eof then
rs.addnew   '会员第一次进入网站(可能从网站首页直接登录进入论坛)。
rs("id")=session.sessionID
rs("name")=session("show") '写入用户昵称
rs("username")=session("username")    '写入登录用户名
rs("online")=1         '表示用户已经登陆,是会员身份
rs("datetime")=now()        '将当前系统时间设置为用户的登陆时间
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If userip = "" Then
        userip= Request.ServerVariables("REMOTE_ADDR")
        end if
  rs("ip")=userip
else  //会员非第一次浏览网站,访客登录网站。
rs("name")=session("show") 更新用户昵称
rs("username")=session("username")
rs("online")=1         '表示用户已经登陆,是会员身份
rs("datetime")=now()
end if
rs.update
rs.close
end if
set rs=nothing
%>
conn.execute("delete from online where datediff('s',datetime,now())>60")  '删除60秒没有活动的访客,时间可以自己调整。
共有 3653 人次浏览 收藏本页 返回上一页 责任编辑:
相关文章
    DrMaragret2010-9-8 9:55:45 认为:
    2, <a href="http://www.runnerbus.com/profiles/blogs/lorazepam-uk-without">Order Lorazepam Cod in Arizona Sawmill</a>, [url="http://www.runnerbus.com/profiles/blogs/lorazepam-uk-without"]Order Lorazepam Cod in Arizona Sawmill[/url], http://www.runnerbus.com/profiles/blogs/lorazepam-uk-without Order Lorazepam Cod in Arizona Sawmill, bqoja, <a href="http://redimi2elfenomeno.com/profiles/blogs/valium-medicine-bad-side">Valium Withdrawal Symptom</a>, [url="http://redimi2elfenomeno.com/profiles/blogs/valium-medicine-bad-side"]Valium Withdrawal Symptom[/url], http://redimi2elfenomeno.com/profiles/blogs/valium-medicine-bad-side Valium Withdrawal Symptom, 837, <a href="http://www.jamespot.com/a/2013856-AMBIEN-AMBIEN-Dosage-Generic-AMBIEN-Without-Prescription.html">Buy Ambien Online No Membership</a>, [url="http://www.jamespot.com/a/2013856-AMBIEN-AMBIEN-Dosage-Generic-AMBIEN-Without-Prescription.html"]Buy Ambien Online No Membership[/url], http://www.jamespot.com/a/2013856-AMBIEN-AMBIEN-Dosag
    DrSharyn2010-9-8 9:52:08 认为:
    1, <a href="http://www.runnerbus.com/profiles/blogs/lorazepam-uk-without">Lorazepam Non Rx For Next Day Delivery</a>, [url="http://www.runnerbus.com/profiles/blogs/lorazepam-uk-without"]Lorazepam Non Rx For Next Day Delivery[/url], http://www.runnerbus.com/profiles/blogs/lorazepam-uk-without Lorazepam Non Rx For Next Day Delivery, vqtpf, <a href="http://redimi2elfenomeno.com/profiles/blogs/valium-medicine-bad-side">Order Valium Online No Prescription in Delaware</a>, [url="http://redimi2elfenomeno.com/profiles/blogs/valium-medicine-bad-side"]Order Valium Online No Prescription in Delaware[/url], http://redimi2elfenomeno.com/profiles/blogs/valium-medicine-bad-side Order Valium Online No Prescription in Delaware, 8[, <a href="http://www.jamespot.com/a/2013856-AMBIEN-AMBIEN-Dosage-Generic-AMBIEN-Without-Prescription.html">Where To Buy Ambien By Cod</a>, [url="http://www.jamespot.com/a/2013856-AMBIEN-AMBIEN-Dosage-Generic-AMBIEN-Without-Prescription.html"]Where To Buy Ambien By Cod[/url]
    DrKeira2010-9-8 9:40:19 认为:
    5, <a href="http://www.jamespot.com/a/2002284-Fioricet-Fioricet-On-Line-Buy-Online-Fioricet.html">Buy Fioricet in Brampton</a>, [url="http://www.jamespot.com/a/2002284-Fioricet-Fioricet-On-Line-Buy-Online-Fioricet.html"]Buy Fioricet in Brampton[/url], http://www.jamespot.com/a/2002284-Fioricet-Fioricet-On-Line-Buy-Online-Fioricet.html Buy Fioricet in Brampton, qpjzpa, <a href="http://www.recessioncrusher.com/profiles/blogs/discount-xanax-discount">Xanax Buy in Us No Prescrsneeded</a>, [url="http://www.recessioncrusher.com/profiles/blogs/discount-xanax-discount"]Xanax Buy in Us No Prescrsneeded[/url], http://www.recessioncrusher.com/profiles/blogs/discount-xanax-discount Xanax Buy in Us No Prescrsneeded, xrahfv, <a href="http://clydecomedy.ning.com/profiles/blogs/ordering-viagra-cheap-viagra">Order Viagra Uk At Delaware</a>, [url="http://clydecomedy.ning.com/profiles/blogs/ordering-viagra-cheap-viagra"]Order Viagra Uk At Delaware[/url], http://clydecomedy.ning.com/profiles/blogs/order
    DrRosena2010-9-8 9:36:47 认为:
    3, <a href="http://www.jamespot.com/a/2002284-Fioricet-Fioricet-On-Line-Buy-Online-Fioricet.html">Order Buy Fioricet Er Online</a>, [url="http://www.jamespot.com/a/2002284-Fioricet-Fioricet-On-Line-Buy-Online-Fioricet.html"]Order Buy Fioricet Er Online[/url], http://www.jamespot.com/a/2002284-Fioricet-Fioricet-On-Line-Buy-Online-Fioricet.html Order Buy Fioricet Er Online, :-OO, <a href="http://www.recessioncrusher.com/profiles/blogs/discount-xanax-discount">Cheap Xanax With Cash On Delivery</a>, [url="http://www.recessioncrusher.com/profiles/blogs/discount-xanax-discount"]Cheap Xanax With Cash On Delivery[/url], http://www.recessioncrusher.com/profiles/blogs/discount-xanax-discount Cheap Xanax With Cash On Delivery, oeursp, <a href="http://clydecomedy.ning.com/profiles/blogs/ordering-viagra-cheap-viagra">Chance To Order Cheap Viagra Er Online</a>, [url="http://clydecomedy.ning.com/profiles/blogs/ordering-viagra-cheap-viagra"]Chance To Order Cheap Viagra Er Online[/url], http://clydec
    DrCory2010-9-8 9:24:51 认为:
    4, <a href="http://beachpatrol.com/profiles/blogs/ativan-without-prescription">Where Can I Buy Ativan Without Prescription</a>, [url="http://beachpatrol.com/profiles/blogs/ativan-without-prescription"]Where Can I Buy Ativan Without Prescription[/url], http://beachpatrol.com/profiles/blogs/ativan-without-prescription Where Can I Buy Ativan Without Prescription, >:))), <a href="http://www.rockforhunger.org/profiles/blogs/site-about-zolpidem-info-on">Buy Zolpidem Online Next Day Delivery</a>, [url="http://www.rockforhunger.org/profiles/blogs/site-about-zolpidem-info-on"]Buy Zolpidem Online Next Day Delivery[/url], http://www.rockforhunger.org/profiles/blogs/site-about-zolpidem-info-on Buy Zolpidem Online Next Day Delivery, kpe, <a href="http://www.translatorsgroup.com/profiles/blogs/buying-cheap-phentermine-pill">Order Phentermine in Tulsa</a>, [url="http://www.translatorsgroup.com/profiles/blogs/buying-cheap-phentermine-pill"]Order Phentermine in Tulsa[/url], http://www.translatorsgrou