
29/07/2005, 16:39
|
 | Moderador | | Fecha de Ingreso: enero-2002
Mensajes: 8.031
Antigüedad: 23 años, 5 meses Puntos: 98 | |
Si, estoy de acuerdo contigo con respecto a las cookies, pero despues de analizarlo creo que no nos importa mucho que solo se guarde un hit por cada pagina que venga de la misma IP...y obviamente se necesitan las cookies habillitadas, este es el codigo por si a alguien le sirve:
Optimizado para MS SQL
DB estructura:
tbl_visit
- visit_ip | varchar | 50
- page_id | int | 4
- visit_date | date | 8
tbl_visit_exclude
- visit_exclude_ip | varchar | 50
- visit_exclude_three_octets | bit | 1
Código:
<%
Const sep = ":."
Class Visit
Private m_record_page
Private m_client_ip
Public Property Get record_page()
record_page = m_record_page
End Property
Public Property Let record_page(p_data)
m_record_page = p_data
End Property
Public Property Get client_ip()
client_ip = m_client_ip
End Property
Public Property Let client_ip(p_data)
m_client_ip = p_data
End Property
Public Function isIPExcluded()
Dim boolIPExcluded
Dim pos
Dim strTempIP
Dim strTempClientIP
strTempIP = ""
strTempClientIP = ""
boolIPExcluded = false
ds = GetExcludedIPs()
if isArray(ds) then
strTempClientIP = ""
pos = instrRev(m_client_ip, ".")
strTempClientIP = left(m_client_ip, pos)
for j =0 to uBound(ds,2)
strTempIP = ""
if ds(1,j) then
'three octets
'we need to parse our IP here
arrTempIP = split(ds(0,j), ".")
if uBound(arrTempIP) => 2 then
strTempIP = ""
for i = 0 to 2
strTempIP = strTempIP & arrTempIP(i) & "."
next
else
erase arrTempIP
exit for
end if
erase arrTempIP
if strTempIP = strTempClientIP then
boolIPExcluded = true
exit for
end if
else
'regular IP
if cstr(ds(0,j)) = cstr(m_client_ip) then
boolIPExcluded = true
exit for
end if
end if
next
erase ds
end if
isIPExcluded = boolIPExcluded
End Function
Public Sub RecordVisit()
Dim pages_visited
Dim boolPageFound
pages_visited = Request.Cookies("visit")("pages_visited")
if len(pages_visited) > 0 then
'User has been here today
boolPageFound = false
arrPagesVisited = split(pages_visited, sep)
for j = 0 to uBound(arrPagesVisited)
if cstr(arrPagesVisited(j)) = cstr(m_record_page) then
boolPageFound = true
exit for
end if
next
erase arrPagesVisited
if not boolPageFound then
'New visit to this page, we have to update the cookie
'Look for IP Address Excluded
if not isIPExcluded() then
Call RecordValues(m_record_page, m_client_ip, pages_visited)
end if
end if
else
'First time this page will be recorded today
'Look for IP Address Excluded
if not isIPExcluded() then
Call RecordValues(m_record_page, m_client_ip, pages_visited)
end if
end if
End Sub
Private Function GetExcludedIPs()
Dim ObjConn
Dim rs
Dim cmd
Dim qry
Set ObjConn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
Set cmd = Server.CreateObject("ADODB.Command")
ObjConn.Open strConnect
qry = "SELECT visit_exclude_ip, visit_exclude_three_octets FROM tbl_visit_exclude"
cmd.ActiveConnection = ObjConn
cmd.CommandText = qry
cmd.CommandType = adCmdText
rs.CursorType = 3
rs.Open cmd
if not rs.EOF then
ds = rs.GetRows()
else
ds = Null
end if
ObjConn.Close
Set ObjConn = Nothing
Set rs = Nothing
Set cmd = Nothing
GetExcludedIPs = ds
End Function
Private Sub RecordValues(thispage, thisip, strPagesVisited)
Dim ObjConn
Dim param
Dim cmd
Dim qry
Set ObjConn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
ObjConn.Open strConnect
qry = "INSERT INTO tbl_visit VALUES(?,?,?) "
Set param = cmd.CreateParameter("visit_ip", adVarChar, adParamInput, 50, thisip)
cmd.Parameters.Append(param)
Set param = cmd.CreateParameter("page_id", adInteger, adParamInput, 4, thispage)
cmd.Parameters.Append(param)
Set param = cmd.CreateParameter("visit_date", adDate, adParamInput, 8, Now())
cmd.Parameters.Append(param)
cmd.ActiveConnection = ObjConn
cmd.CommandText = qry
cmd.CommandType = adCmdText
cmd.Execute()
ObjConn.Close
Set ObjConn = Nothing
Set cmd = Nothing
Set param = Nothing
if len(strPagesVisited) > 0 then
Response.Cookies("visit")("pages_visited") = strPagesVisited & sep & page
else
Response.Cookies("visit")("pages_visited") = page
end if
Response.Cookies("visit").Path = "/"
Response.Cookies("visit").Expires = DateAdd("d", 1, Date())
End Sub
End Class
%>
y mas o menos se llamaria asi:
Código:
page = request.querystring("page")
Set ObjVisit = New Visit
ObjVisit.client_ip = Request.ServerVariables("REMOTE_ADDR")
ObjVisit.record_page = page
if len(ObjVisit.client_ip) > 0 and ObjVisit.record_page > 0 then
Call ObjVisit.RecordVisit()
end if
Set ObjVisit = Nothing
Felices Trazos! je je
__________________ "El hombre que ha empezado a vivir seriamente por dentro, empieza a vivir más sencillamente por fuera."
-- Ernest Hemingway
Última edición por u_goldman; 29/07/2005 a las 16:58 |