Forum Moderators: open

Message Too Old, No Replies

global.asa ban IP

         

jlander

9:22 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Hello,

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 = nothing

end 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]

Easy_Coder

10:20 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This probably isn't going to work in Application Start
>>Request.ServerVariables("Remote_Addr")

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.

jlander

1:06 am on Nov 22, 2005 (gmt 0)

10+ Year Member



You are correct. That should have been session_onstart. I'm on a shared server and I do not have access to IIS, so I have to find a way to do it myself.

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.

mattglet

12:13 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just remove all your Application_OnStart stuff, and replace it with this (just make sure you keep your <server> tags):

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

jlander

1:13 pm on Nov 22, 2005 (gmt 0)

10+ Year Member



Thanks, I got it to work (does not cause any errors), but it didn't behave the way I wanted. I believe the response.redirect is causing a loop.

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?

Staffa

4:31 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can keep

Response.Redirect "badbot.asp"

however, do not put badbot.asp on your server, then the bot will get a 404 and move on. Else the bot will loop, sometimes for a long time, between trying to get access and your badbot file.

jlander

12:51 am on Nov 23, 2005 (gmt 0)

10+ Year Member



Thanks Everyone,

It is working like a champ.