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 ";
}