Determine the specific address based on the Client IP Address

Source: Internet
Author: User
Tags servervariables

Today I have studied how to determine the specific address based on the Client IP address. For classification information such as 58 city, the website uses the switched city to determine the specific address based on the Client IP address.

Database access. The IP1 and ip2 fields in the table are in decimal format. You need to convert the IP address obtained from the client to decimal.
Code:
// Convert the Client IP address to decimal
Public long convertip (string dotip)
{
String [] subip = dotip. Split ('.');
Long IP = 256*256*256 * int64.parse (subip [0]) + 256*256 * int64.parse (subip [1]) + 256 * int64.parse (subip [2]) + int64.parse (subip [3]);
Return IP;
}

Here, the dotip is the Client IP address (note the client's use of the proxy server)
String dotip = request. servervariables ["http_x_forwarded_for"];
If (string = NULL)
{
Dotip = request. servervariables ["remote_addr"];
}
You can also directly obtain:

String dotip = system. Web. httpcontext. Current. Request. userhostaddress;

The database query statement can be:

String SQL = string. format ("select ip_province, ip_city from iptocity where IP1 <= {0} And ip2 >={ 1}", convertip (textbox1.text), convertip (textbox1.text ));
Dbconn conn = new dbconn ();
Oledbdatareader ODR = conn. getdatareader (SQL );
If (ODR. Read ())
{
Label3.text = ODR ["ip_province"]. tostring () + "-" + ODR ["ip_city"]. tostring ();
Response. Redirect ("city. aspx? Ip_city = "+ ODR [" ip_city "]. tostring () + "");

Response. Cookies ["ip_city"]. value = label1.text;
}
Else
{
Label3.text = "Beijing-Beijing ";
}

 

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.