<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <% dim finishgetip,showip,allip '//////////////////////////////////////////////////////////////////////////////////////// '程序還不是很精簡,以后再修改 '本程序所用的數據庫為-- “馮志宏”-- 所寫的--“追捕”--軟件中所帶IP數據庫和 ' “國華軟件 Guohua Soft”的作者 --“馮國華”—所寫的“全球IP地址分配表.chm”合二為一得到的 '感謝“馮志宏”和“馮國華”提供的數據 '數據庫中還有不少的重復IP地址,希望有心人能將其刪除,減小數據庫 '我的程序寫的還很笨拙,希望大家能多提意見,多多交流,謝謝! '//////////////////////////////////////////////////////////////////////////////////////// '解決思路: '取得的客戶端IP一般是202.11.25.1這種,而數據庫中的IP格式為202.011.025.001,這就需要將取得的 '客戶端IP轉換為與數據庫中IP一樣的格式 '因為目前我們所用的IP是分為4段,每段3位,中間以“.”分隔 '所以我的思路是將客戶端IP以“.”符號分割為4段,即202/11/25/1 '然后再分別核對每一段,如果是3位,則不變;如不足3位,為2位,該段前補1個0,為1,同理,則補2個0 '得到格式化后的IP后,去掉IP的最后一段,即取包括“.”的前11位,與數據庫中的startip字段的前11位相比較,查找相同的值 '因為從數據庫中可以看到,startip和endip的前三段都是一樣的,而最后一段不過是內部子網地址,可以去掉 '所以只要取startip或endip的任意一個字段的前11位與客戶端IP的前11位相比較就可以查到正確的所在地 '///////////////////////////////////////////////////////////////////////////////////////// function checkip_trueip() '取客戶端真實IP getclientip = Request.ServerVariables("HTTP_X_FORWARDED_FOR") '如果客戶端用了代理服務器,則用Request.ServerVariables("REMOTE_ADDR")方法只能得到空值,則應該用ServerVariables("HTTP_X_FORWARDED_FOR")方法 If getclientip = "" Then getclientip = Request.ServerVariables("REMOTE_ADDR")'如果客戶端沒用代理,則Request.ServerVariables("HTTP_X_FORWARDED_FOR")得到是空值,應該用Request.ServerVariables("REMOTE_ADDR")方法 end if checkip_trueip = getclientip end function '///////////////////////////////////////////////////////////////////////////// function getaccessrecordset(db,sql,mark,read)'取得Recordset對象 set conn=getaccessconn(db)'輸入參數為db-數據庫的相對路徑,sql-SQL語句,mark,read為數據庫讀取方式,1,1為只讀,1,3為讀寫 'constr="Provider=microsoft.jet.oledb.4.0;"&"data Source="&Server.MapPath(db) ' conn.open constr set getaccessrecordset=server.CreateObject("ADODB.Recordset") getaccessrecordset.open sql,conn,mark,read End function '////////////////////////////////////////////////////////////////////////// function getaccessconn(db)'取得connection對象 set getaccessconn=server.CreateObject("ADODB.Connection") 'constr="DRIVER={MICROSOFT ACCESS DRIVER (*.MDB)};DBQ="&SERVER.MAPPATH("allcon/#bbsall.mdb") constr="Provider=microsoft.jet.oledb.4.0;"&"data Source="&Server.MapPath(db) getaccessconn.open constr end function '///////////////////////////////////////////////////////////////////////// dim getip 'getip=(trim(request.ServerVariables("REMOTE_ADDR")))'從客戶端獲取IP 'getip=(trim(request.QueryString("comes"))) '自己輸入IP測試 'response.Write(getip&"<br>") '//////////////////////////////////////////////////////////////////////// function checkip_locations(checkstring) '返回IP中分隔字符的位置函數 checkip_locations=Instr(checkstring,".") '將位置的值賦予給函數 end function '/////////////////////////////////////////////////////////////////////// '以下函數為分割IP,取得每次分割后“.”符號右邊的IP剩余的字符串 function checkip_left(checkstring) locations_left=checkip_locations(checkstring) '得到在IP剩余的字符串中“.”第一次出現的位置 iplength_left=Len(checkstring) '取得IP剩余的字符串的長度 divide_locations_left=iplength_left-locations_left '取得在IP剩余的字符串中“.”第一次出現的位置,從右往左數是多少位 ipstr_left=Right(checkstring,divide_locations_left) '取得本次分割后,“.”符號右邊的IP剩余的字符串 checkip_left=ipstr_left '將上面得到的字符串賦給函數 end function '/////////////////////////////////////////////////////////////////////// '以下函數為分割IP,取得每次分割后“.”符號左邊的IP字符串,即將IP分為四段,每一段的字符串 function checkip_right(checkstring) locations_right=checkip_locations(checkstring) '取得在IP中“.”第一次出現的位置 iplength_right=Len(checkstring) '取得IP字符串長度 divide_locations_right=iplength_right-locations_right '取得在IP剩余的字符串中“.”第一次出現的位置,從右往左數是多少位 ipstr11=Trim(Replace(Left(checkstring,locations_right),".","")) '將得到的“.”左邊的字符串去掉"."符號 '如果IP分為4段后每一段不足3位則補0 if Len(ipstr11)="2" then ipstr11="0"&ipstr11 if Len(ipstr11)="3" then ipstr11=ipstr11 if Len(ipstr11)="1" then ipstr11="00"&ipstr11 checkip_right=ipstr11 '得到“.”符號之前的字符串,即本次分割后得到的IP分割為四段后其中的一段 end function '////////////////////////////////////////////////////////////////////// '檢查IP是否為內部網IP '我寫的判斷是以:127.0.0.0-127.XXX.XXX.255和192.0.0.0-192.XXX.XXX.255為依據,如果為這二者,則是內部網IP,反之為外部網 '判斷內部IP的依據是什么,我也不清楚,所以這里要高手多多指點,并加以修正,與我聯系 function checkiplocal(checkstring) dim re1 set re1=new RegExp '取得正則表達式對象 're1.pattern中的表達式為,內部網的IP應為127或192開頭,中間為0-9中任意1-3個數字加"."組成一段 re1.pattern="^(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$" re1.global=false re1.Ignorecase=false checkiplocal=re1.test(checkstring) set re1=nothing end function '////////////////////////////////////////////////////////////////////// function checkip_remote(checkstring) dim iplength 'IP字符串的長度 dim locations '"."字符出現的位置 iplength=Len(checksting) locations=Instr(checkstring,".") '從左到右檢索“.”符號在IP字符串中第一次出現的位置 '以“.”字符將IP分割為4段 locations2=iplength-locations ipstring1=Left(checkstring,locations) ipstring2=Right(checkstring,locations2) end function '////////////////////////////////////////////////////////////////////// '/////////////////////////////////////////////////////////////////////// ipinfo_local="您的IP是內部網IP!" ipinfo_remote="外部網IP!" getip=checkip_trueip() currentip=checkiplocal(getip) '調用checkiplocal()函數對得到的IP進行檢查,確定是內部網地址還是外部網地址 'if currentip=true then'測試代碼 'response.Write(ipinfo_local) if currentip=true then '為假 response.Write(ipinfo_local)'說明為內部網IP else '進行轉換 '以下為循環提取并按位補0將IP分為4段 locations=checkip_locations(getip)'取得“.”在第一次分割前在IP中第一次出現的位置 iplength=Len(getip) '取得客戶端IP的長度 divide_locations=iplength-locations '取得將客戶端IP從右向左數到IP從左往右數第一個“.”的位置 ipstr1=Trim(Replace(Left(getip,locations),".","")) ipstr2=Right(getip,divide_locations)'取得第一次分割后客戶端右邊剩下的數值 '如果IP分為4段后每一段不足3位則補0 if Len(ipstr1)="2" then ipstr1="0"&ipstr1 '長度為二,不足三位,在字符串之前補一個0 if Len(ipstr1)="3" then ipstr1=ipstr1 '據上類推 if Len(ipstr1)="1" then ipstr1="00"&ipstr1 '這時的ipstr1為IP的第一段 ipstr12=checkip_right(ipstr2) '這時的ipstr12為IP的第二段 ipstr122=checkip_left(ipstr2) ipstr13=checkip_right(ipstr122) '這時的ipstr13為IP的第三段 ipstr14=checkip_left(ipstr122) '這時的ipstr14為IP的第四段 if Len(ipstr14)="1" then ipstr14="00"&ipstr14 '對得到的IP的第四段進行補0,此步驟可不要 if Len(ipstr14)="2" then ipstr14="0"&ipstr14 if Len(ipstr14)="3" then ipstr14=ipstr14 'response.write ipstr1&"<br>" '寫出IP分割后的每段的值 'response.write ipstr12&"<br>" 'response.write ipstr13&"<br>" 'response.write ipstr14 allip=ipstr1&"."&ipstr12&"."&ipstr13&"."&ipstr14 finishgetip=left(allip,11) dim ipaddr,iplocal,sqls '以下SQL語句為提取startip字段左邊11位值是否等于客戶端IP的左邊的11位的值 sqls="SELECT country_state,areauser FROM ip WHERE Left(startip,11)='"&finishgetip&"'" set rs=getaccessrecordset("#worldip.mdb",sqls,"1","1") '得到查詢值 if rs.eof then '如果沒找到與客戶端IP相等的值 showip=checkip_trueip() '把客戶端IP賦予showip ipaddr="未知地區" '國家或省份 iplocal="未知地點" '具體的地方 else showip=checkip_trueip() ipaddr=rs("country_state") iplocal=rs("areauser") end if 'response.write("您的IP是:"&showip&" ") 'response.write("您來自:"&ipaddr&" ") 'response.write("您是:"&iplocal) rs.close set rs=nothing %> <%="您的IP是:"&showip&" "%> <%="您來自:"&ipaddr&" "%> <%="您是:"&iplocal&"<br>"%> 如果IP地址有錯誤,請與我聯系,或下載數據庫更正,謝謝!<br> <table width="760" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="203"><a href="Script56.rar">下載Script56.CHM</a>-->1.34M</td> <td width="548">簡介:Microsoft的幫助文檔,有VBscript語法,JScript語法,正則表達式 </td> <td width="3"> </td> <td width="6"> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><a href="ipsearch.rar">下載ASP全球IP地址搜索程序</a></td> <td>ASP+ACCESS 大小401K;格式rar</td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><font color="#000099"> </font> <font color="#0000FF"> </font></td> <td> </td> <td> </td> </tr> <tr> <td>如果你的IP是未知,如果你愿意,請提交你的所在地:</td> <td> <form name="form1" method="post" action="postip.asp"> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="21%"> 省份: </td> <td width="44%"> <input type="text" name="country_state"> </td> <td width="35%"> </td> </tr> <tr> <td width="21%">具體所在地或是什么網的用戶:</td> <td width="44%"> <input type="text" name="areauser"> </td> <td width="35%">例如:北京清華大學或北京網通用戶</td> </tr> <tr> <td width="21%"> </td> <td width="44%"> <input type="hidden" name="startip" value="<%=finishgetip&".000"%>"> <input type="hidden" name="endip" value="<%=finishgetip&".255"%>"> </td> <td width="35%"> </td> </tr> <tr> <td width="21%"> </td> <td width="44%"> <input type="submit" name="Submit" value="提交"> </td> <td width="35%"> </td> </tr> </table> </form> </td> <td> </td> <td> </td> </tr> </table> <% end if %> </body> </html> 演示地址:http://www.knowsky.com/ip/searchip2.asp 如需源代碼,可發信向站長所取:knowsky@21cn.com
|