Forum Moderators: open
I have a database of bad bot IP addresses of bots that do not honor my robots.txt that I've been collecting. I'd like to ban these IPs from access to the site. I've grasped basic ASP, but can't find a good resource for writing a valid global.asa. Can someone look at what I've got and give me a few pointers?
<script language="VBScript" runat="Server">sub Application_OnStart
Dim conn, rs, sql, sIP, tIP, myIP
myIP = "xx.xx.xx.xx" ' Set my IP so I do not ban myself.'Create an ADO connection and recordset object
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
'Set an active connection and select fields from the database
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\websites\example\example.com\db\badbot.mdb"
sql = "SELECT IP FROM botID;"
rs.CursorType = 2
rs.LockType = 3
rs.Open sql, conn'Read user IP
sIP = Request.ServerVariables("Remote_Addr")
' check to see if user is my IP
if NOT(sIP = myIP) then
' Check to see if the bot IP is in the database.
Do While not Rs.EOF
tIP = rs("IP")
if sIP = tIP then
rs.Close
Set rs = nothing
Set conn = nothing
Response.Redirect "badbot.asp"
else
Rs.MoveNext
end if
Loop
end if
rs.Close
Set rs = nothing
Set conn = nothingend sub
</script>
The error I'm getting is on the bold line:
error '8002802b'
Element not found.
//global.asa, line 21
[edited by: Woz at 9:25 pm (utc) on Nov. 21, 2005]
[edit reason] examplified URLs [/edit]
That's not really a user specific place like the Session On_Start. Be very careful about the overhead you take-on in the Session On_Start Sub. That can have a dramatic impact on your site.
Another option would be to load your banned IP list into IIS (IP Address Restrictions) if it's not a large list. I don't know how to bulk load the list if it is.
I've done a lot of reading here and if I understand correctly, the global.asa session_onstart is the way to go because it will only execute once for each human visitor. Unfortunately, I've not found a good resource that explains what to do.
The other way I saw to do this would be to call the redirect script at the very beginning of each of my asp pages. This seems, to me, the less efficient way to do it because the code would be executed for every page.
I'm pretty much a novice to asp and programming, so I could be totally wrong and would appreciate it if anyone can give me any clues.
sub Session_OnStart
Dim conn, rs, sql, sIP, tIP, myIP
myIP = "xx.xx.xx.xx" ' Set my IP so I do not ban myself.
'Create an ADO connection and recordset object
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
'Set an active connection and select fields from the database
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\websites\example\example.com\db\badbot.mdb"
sql = "SELECT IP FROM botID;"
rs.CursorType = 2
rs.LockType = 3
rs.Open sql, conn
'Read user IP
sIP = Request.ServerVariables("Remote_Addr")
' check to see if user is my IP
if NOT(sIP = myIP) then
' Check to see if the bot IP is in the database.
Do While not Rs.EOF
tIP = rs("IP")
if sIP = tIP then
rs.Close
Set rs = nothing
Set conn = nothing
Response.Redirect "badbot.asp"
else
Rs.MoveNext
end if
Loop
end if
rs.Close
Set rs = nothing
Set conn = nothing
end sub
I changed
Response.Redirect "badbot.asp"
to
Response.status = "403 Forbidden"
Response.End
but that is not working either. The page doesn't display, but the header still shows 200. Any ideas?