ASP IP Transfer City database code download

Source: Internet
Author: User


<%
Dim ip_address,ip_rs,cip,ip_conn,ip_connstr,ip_province,ip_city
Const datatype=1 ' 0 is an Access database, and 1 is a SQL Server database
Const mdbpath= "Ip_address.mdb" ' If it is a SQL Server database, this entry is left 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+1
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
If err.number<>0 Then
Err.Clear
Response.Write ("Database connection failed!") ")
Response.End ()
End If
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 city,province from ip_address where" &CIP& ">=ip1 and" &CIP& "<=IP2 ORDER by ID Desc ")
  if ip_rs. Eof Then
   ip_province= "Beijing"
   ip_city= "Beijing"
  else
    ip_province=ip_rs (1)
   ip_city=ip_rs (0)
  end if
  IP_ Rs. Close
  set ip_rs=nothing
  ip_conn. Close
  set ip_conn=nothing
 else
  ip_province= "Beijing"
  ip_city= " Beijing "
 end If
End Function
%>
Call method
<!--#include file=" function.asp "
<%
Cal L getregion ()
Response.Write (ip_province& "-&ip_city)
%>

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.