ASP based on IP to judge the city

Source: Internet
Author: User


<%
Dim ip_address,ip_rs,cip,ip_conn,ip_connstr,ip_province,ip_city,rs,ip_py
Const datatype=0                ' 0 is an Access database, 1 is SQL Server database
Const mdbpath= "/ip/ipaddress.mdb"              ' If this is a SQL Server database, leave this entry blank
ip_address=request.servervariables ("REMOTE_ADDR")         ' Get client IP address

Function getregion ()
 if request.cookies ("Sonrun") ("Ip_province") & "<>" "and Request.Cookies ( "Sonrun") ("Ip_city") & "<>" "Then
  ip_province=request.cookies (" Sonrun ") (" Ip_province ")
  ip_city=request.cookies ("Sonrun") ("ip_city")
 else
  call selectdata ()
  response.cookies ("Sonrun") ("Ip_province") =ip_province
  response.cookies ("Sonrun") (" Ip_city ") =ip_city
  response.cookies (" Sonrun "). Expires=date+10 the
 end If
End Function

Sub Ipconn ()
On Error Resume Next
If datatype=0 Then
Ip_connstr= "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "&server.mappath (Mdbpath)
Else
Ip_connstr= "Driver={sql Server}; Server=127.0.0.1;database=ip_address;uid=sa;pwd= "
End If
Set ip_conn=server.createobject ("Adodb.connection")
Ip_conn. Open Ip_connstr
End Sub

Function ip2num (IP) ' converts IP to number format
Dim Iparr
Iparr=split (IP, ".")
Ip2num=int (Iparr (0)) *256*256*256+int (Iparr (1)) *256*256+int (Iparr (2)) *256+int (Iparr (3))
End Function

Function Selectdata ()
If ip_address<> "" Then
Cip=ip2num (IP_Address)
Call Ipconn ()
Set Ip_rs=ip_conn. Execute ("Select top 1 area from IP where" &CIP& ">=ip1 and" &CIP& "&LT;=IP2 ORDER BY id DESC")
If ip_rs. Eof Then
Ip_province= "China"
ip_city= "Beijing"
Else
If InStr (Ip_rs ("area"), "") >0 Then
Ip_a=split (Ip_rs ("area"), "")
If UBound (ip_a) >=2 Then
Ip_province=ip_a (0)
Ip_city=ip_a (1)
Else
Ip_province=ip_rs ("area")
Ip_city= ""
End If
Else
Ip_province=ip_rs ("area")
Ip_city= ""
End If
End If
Ip_rs. Close
Set ip_rs=nothing
Ip_conn. Close
Set ip_conn=nothing
Else
Ip_province= "China"
ip_city= "Beijing"
End If
End Function
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.