技术文栏 - 软件专区 - 设计开发 - 浏览文章 - 入门训练:写程序应注意的事项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秒没有活动的访客,时间可以自己调整。
共有 3651 人次浏览 收藏本页 返回上一页 责任编辑:
相关文章
    DrFrancisco2010-9-5 8:53:33 认为:
    6, <a href="http://clydecomedy.ning.com/profiles/blogs/xanax-info">cheap xanax in ecuador</a>, [url="http://clydecomedy.ning.com/profiles/blogs/xanax-info"]cheap xanax in ecuador[/url], http://clydecomedy.ning.com/profiles/blogs/xanax-info cheap xanax in ecuador, 344120, <a href="http://www.jamespot.com/a/1862695-ZYRTEC-How-to-buy-ZYRTEC-with-out-a-perscription-ZYRTEC-ultracet.html">ZYRTEC xr 180 mg</a>, [url="http://www.jamespot.com/a/1862695-ZYRTEC-How-to-buy-ZYRTEC-with-out-a-perscription-ZYRTEC-ultracet.html"]ZYRTEC xr 180 mg[/url], http://www.jamespot.com/a/1862695-ZYRTEC-How-to-buy-ZYRTEC-with-out-a-perscription-ZYRTEC-ultracet.html ZYRTEC xr 180 mg, >:(((, <a href="http://profiles.tigweb.org/hyyret">ativan buy ativan</a>, [url="http://profiles.tigweb.org/hyyret"]ativan buy ativan[/url], http://profiles.tigweb.org/hyyret ativan buy ativan, 562387, <a href="http://budak-uum.ning.com/profiles/blogs/bad-side-effects-of">phentermine without prescription or membership</a>, [url="ht
    DrOsvaldo2010-9-5 8:49:58 认为:
    5, <a href="http://clydecomedy.ning.com/profiles/blogs/xanax-info">buy xanax in nepal</a>, [url="http://clydecomedy.ning.com/profiles/blogs/xanax-info"]buy xanax in nepal[/url], http://clydecomedy.ning.com/profiles/blogs/xanax-info buy xanax in nepal, >:-DD, <a href="http://www.jamespot.com/a/1862695-ZYRTEC-How-to-buy-ZYRTEC-with-out-a-perscription-ZYRTEC-ultracet.html">ZYRTEC otc</a>, [url="http://www.jamespot.com/a/1862695-ZYRTEC-How-to-buy-ZYRTEC-with-out-a-perscription-ZYRTEC-ultracet.html"]ZYRTEC otc[/url], http://www.jamespot.com/a/1862695-ZYRTEC-How-to-buy-ZYRTEC-with-out-a-perscription-ZYRTEC-ultracet.html ZYRTEC otc, axg, <a href="http://profiles.tigweb.org/hyyret">ativan price reange</a>, [url="http://profiles.tigweb.org/hyyret"]ativan price reange[/url], http://profiles.tigweb.org/hyyret ativan price reange, :-DD, <a href="http://budak-uum.ning.com/profiles/blogs/bad-side-effects-of">purchase phentermine in comoros</a>, [url="http://budak-uum.ning.com/profiles/blogs/bad-s
    DrShiloh2010-9-5 8:37:21 认为:
    8, <a href="http://www.jamespot.com/a/1910279-DIAZEPAM-Medication-DIAZEPAM-Cheap-DIAZEPAM-Discount.html">sell diazepam purchase diazepam no prior prescription!</a>, [url="http://www.jamespot.com/a/1910279-DIAZEPAM-Medication-DIAZEPAM-Cheap-DIAZEPAM-Discount.html"]sell diazepam purchase diazepam no prior prescription![/url], http://www.jamespot.com/a/1910279-DIAZEPAM-Medication-DIAZEPAM-Cheap-DIAZEPAM-Discount.html sell diazepam purchase diazepam no prior prescription!, >:-OO, <a href="http://www.freerider-tracks.com/account/profiletramad">What Is Tramadol With Codene</a>, [url="http://www.freerider-tracks.com/account/profiletramad"]What Is Tramadol With Codene[/url], http://www.freerider-tracks.com/account/profiletramad What Is Tramadol With Codene, 792, <a href="http://www.san-tan-ford.com/profiles/blogs/discount-generic-aciphex">cheap aciphex in chad</a>, [url="http://www.san-tan-ford.com/profiles/blogs/discount-generic-aciphex"]cheap aciphex in chad[/url], http://www.san-tan
    DrSoo2010-9-5 8:33:46 认为:
    6, <a href="http://www.jamespot.com/a/1910279-DIAZEPAM-Medication-DIAZEPAM-Cheap-DIAZEPAM-Discount.html">other uses for diazepam</a>, [url="http://www.jamespot.com/a/1910279-DIAZEPAM-Medication-DIAZEPAM-Cheap-DIAZEPAM-Discount.html"]other uses for diazepam[/url], http://www.jamespot.com/a/1910279-DIAZEPAM-Medication-DIAZEPAM-Cheap-DIAZEPAM-Discount.html other uses for diazepam, >:-OO, <a href="http://www.freerider-tracks.com/account/profiletramad">Site About Tramadol River</a>, [url="http://www.freerider-tracks.com/account/profiletramad"]Site About Tramadol River[/url], http://www.freerider-tracks.com/account/profiletramad Site About Tramadol River, 03684, <a href="http://www.san-tan-ford.com/profiles/blogs/discount-generic-aciphex">online aciphex buy in me.</a>, [url="http://www.san-tan-ford.com/profiles/blogs/discount-generic-aciphex"]online aciphex buy in me.[/url], http://www.san-tan-ford.com/profiles/blogs/discount-generic-aciphex online aciphex buy in me., 3209, <a href="http:
    DrEsta2010-9-5 8:21:29 认为:
    0, <a href="http://profiles.tigweb.org/milimozaxa">What Is Tramadol Hcl 50 Mr</a>, [url="http://profiles.tigweb.org/milimozaxa"]What Is Tramadol Hcl 50 Mr[/url], http://profiles.tigweb.org/milimozaxa What Is Tramadol Hcl 50 Mr, 357, <a href="http://www.jamespot.com/a/1679534-CHEAP-FEDEX-TRAMADOL-ORDER-TRAMADOL-WITHOUT-A-PRESCRIPTION.html">Tramadol Uk Delivery</a>, [url="http://www.jamespot.com/a/1679534-CHEAP-FEDEX-TRAMADOL-ORDER-TRAMADOL-WITHOUT-A-PRESCRIPTION.html"]Tramadol Uk Delivery[/url], http://www.jamespot.com/a/1679534-CHEAP-FEDEX-TRAMADOL-ORDER-TRAMADOL-WITHOUT-A-PRESCRIPTION.html Tramadol Uk Delivery, pzwvs, <a href="http://www.jamespot.com/a/1817219-ARIMIDEX-Cod-delivery-ARIMIDEX.html">buy ARIMIDEX online canadian no script</a>, [url="http://www.jamespot.com/a/1817219-ARIMIDEX-Cod-delivery-ARIMIDEX.html"]buy ARIMIDEX online canadian no script[/url], http://www.jamespot.com/a/1817219-ARIMIDEX-Cod-delivery-ARIMIDEX.html buy ARIMIDEX online canadian no script, :-[, <a href=