最近用ASP做了一個(gè)小論壇,客戶又要求要統(tǒng)計(jì)在線人數(shù),所以花了點(diǎn)時(shí)間寫一個(gè)在線統(tǒng)計(jì)訪會(huì)員的功能。寫的過程中也查看了許多文檔。自我感覺用的方法能比較準(zhǔn)確的統(tǒng)計(jì)在線人數(shù)。當(dāng)然一定也有不足的地方,希望各位老師糾正。

  特別說明:

  本論壇登錄是采用用戶名登錄,登錄后取得用戶昵稱。整個(gè)網(wǎng)站不顯示用戶名,我想相對(duì)來說安全一點(diǎn)吧,所以有昵稱和用戶名區(qū)別。

  1。建立數(shù)據(jù)庫表
  表名為online
  設(shè)如下字段
  id '用來記錄每一個(gè)訪問都的session.sessionid
  name '如果是訪客,則記錄為訪客。
  online  '如果是訪各為0 如果是會(huì)員1
  datetime '最近活動(dòng)時(shí)間
  username '會(huì)員的登錄用戶名,訪客為空。
  ip '記錄訪問都的登錄IP

  head.asp '向數(shù)據(jù)庫表寫入在線人數(shù),該頁面必須搜入到每一個(gè)用于瀏覽ASP頁面中。


<%
set rs=Server.CreateObject("ADODB.Recordset")
if session("username")="" then 判斷用戶未登錄
sql="select * from online where id='"&session.sessionid&"' "  '判斷這個(gè)sessionid是否存在于數(shù)據(jù)庫表中.
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()  '更新活動(dòng)時(shí)間
rs.update
rs.close
end if
else
sql="select * from online where id='" & session.sessionID & "' or admin='"&session("username")&"'"  '判斷sessionid 或者 用戶名記錄已存在數(shù)據(jù)表中。
rs.Open sql,Conn,1,3
if rs.eof then
rs.addnew   '會(huì)員第一次進(jìn)入網(wǎng)站(可能從網(wǎng)站首頁直接登錄進(jìn)入論壇)。
rs("id")=session.sessionID
rs("name")=session("show") '寫入用戶昵稱
rs("username")=session("username")    '寫入登錄用戶名
rs("online")=1         '表示用戶已經(jīng)登陸,是會(huì)員身份
rs("datetime")=now()        '將當(dāng)前系統(tǒng)時(shí)間設(shè)置為用戶的登陸時(shí)間
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If userip = "" Then
        userip= Request.ServerVariables("REMOTE_ADDR")
        end if
  rs("ip")=userip
else  //會(huì)員非第一次瀏覽網(wǎng)站,訪客登錄網(wǎng)站。
rs("name")=session("show") 更新用戶昵稱
rs("username")=session("username")
rs("online")=1         '表示用戶已經(jīng)登陸,是會(huì)員身份
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秒沒有活動(dòng)的訪客,時(shí)間可以自己調(diào)整。

標(biāo)簽:

相關(guān)文章

隨機(jī)推薦