The user information table is available in most systems. We also know that there are usually two fields, such as the registered IP address and the last logon IP address, to store the IP address at user registration and the last logon IP address.
The address segment of xxx. xxx is displayed in the background, which is difficult to see and does not know the specific geographical location.
Now we can simply implement this function.
I used QQWry. NET, a public component used to read pure IP database. (Thanks for sharing)
Download the latest pure IP address library and obtain QQWry. dat.
Finally let out the Js in the little girl, jquery-1.3.1.js
Create a Web project AjaxIP and add QQWry. dat to App_Data.
Add the component class of QQWry. NET as follows:
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> 1 using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using System. Text. RegularExpressions;
Using System. Net;
Using System. Net. Sockets;
Namespace AjaxIP
{
Public class IPLocation
{
Public string IP {get; set ;}
Public string Country {get; set ;}
Public string Local {get; set ;}
}
Public class QQWryLocator
{
Static Encoding encoding = Encoding. GetEncoding ("GB2312 ");
Private byte [] data;
Int firstStartIpOffset;
Int lastStartIpOffset;
Int ipCount;
Public int Count {get {return ipCount ;}}
Public QQWryLocator (string dataPath)
{
Using (FileStream fs = new FileStream (dataPath, FileMode. Open, FileAccess. Read, FileShare. Read ))
{
Data = new byte [fs. Length];
Fs. Read (data, 0, data. Length );
}
FirstStartIpOffset = (int) data [0] + (int) data [1]) <8) + (int) data [2]) <16) + (int) data [3]) <24 );
LastStartIpOffset = (int) data [4] + (int) data [5]) <8) + (int) data [6]) <16) + (int) data [7]) <24 );
IpCount = (lastStartIpOffset-firstStartIpOffset)/7 + 1;
If (ipCount <= 1)
{
Throw new ArgumentException ("ip FileDataError ");
}
}
Public static uint IpToInt (string ip)
{
// String [] strArray = ip. Split ('.');
// Return (uint. parse (strArray [0]) <24) + (uint. parse (strArray [1]) <16) + (uint. parse (strArray [2]) <8) + uint. parse (strArray [0]);
// Return (uint) IPAddress. HostToNetworkOrder (int) (IPAddress. Parse (ip). Address ));
Byte [] bytes = IPAddress. Parse (ip). GetAddressBytes ();
Return (uint) bytes [3] + (uint) bytes [2]) <8) + (uint) bytes [1]) <16) + (uint) bytes [0]) <24 );
}
Public static string IntToIP (uint ip_Int)
{
Return new IPAddress (ip_Int). ToString ();
}
Public IPLocation Query (string ip)
{
IPAddress address = IPAddress. Parse (ip );
If (address. AddressFamily! = AddressFamily. InterNetwork)
{
Throw new ArgumentException ("non-IPV4 addresses are not supported ");
}
If (IPAddress. IsLoopback (address ))
{
Return new IPLocation () {IP = ip, Country = "Local loopback address", Local = string. Empty };
}
Uint intIP = (uint) IPAddress. HostToNetworkOrder (int) address. Address );
// If (intIP> = IpToInt ("0.0.0.0") & (intIP <= IpToInt ("2.20.0000255 "))) | (intIP> = IpToInt ("64.0.0.0") & (intIP <= IpToInt ("126.255.255.255") |
// (IntIP> = IpToInt ("58.0.0.0") & (intIP <= IpToInt ("60.00000000255 "))))
// If (intIP <= 50331647 | (intIP> = 1073741824 & intIP <= 2130706431) | (intIP> = 973078528 & intIP <= 1023410175 ))
//{
// Return new IPLocation () {IP = ip, Country = "network reserved address", Local = string. Empty };
//}
IPLocation ipLocation = new IPLocation () {IP = ip };
Uint right = (uint) ipCount;
Uint left = 0;
Uint middle = 0;
Uint startIp = 0;
Uint endIpOff = 0;
Uint endIp = 0;
Int countryFlag = 0;
While (left <(right-1 ))
{
Middle = (right + left)/2;
StartIp = GetStartIp (middle, out endIpOff );
If (intIP = startIp)
{
Left = middle;
Break;
}
If (intIP> startIp)
{
Left = middle;
}
Else
{
Right = middle;
}
}
StartIp = GetStartIp (left, out endIpOff );
EndIp = GetEndIp (endIpOff, out countryFlag );
If (startIp <= intIP) & (endIp> = intIP ))
{
String local;
IpLocation. Country = GetCountry (endIpOff, countryFlag, out local );
IpLocation. Local = local;
}
Else
{
IpLocation. Country = "unknown ";
IpLocation. Local = string. Empty;
}
Return ipLocation;
}
Private uint GetStartIp (uint left, out uint endIpOff)
{
Int leftOffset = (int) (firstStartIpOffset + (left * 7 ));
EndIpOff = (uint) data [4 + leftOffset] + (uint) data [5 + leftOffset]) <8) + (uint) data [6 + leftOffset]) <16 );
Return (uint) data [leftOffset] + (uint) data [1 + leftOffset]) <8) + (uint) data [2 + leftOffset]) <16) + (uint) data [3 + leftOffset]) <24 );
}
Private uint GetEndIp (uint endIpOff, out int countryFlag)
{
CountryFlag = data [4 + endIpOff];
Return (uint) data [endIpOff] + (uint) data [1 + endIpOff]) <8) + (uint) data [2 + endIpOff]) <16) + (uint) data [3 + endIpOff]) <24 );
}
/// <Summary>
/// Gets the country.
/// </Summary>
/// <Param name = "endIpOff"> The end ip off. </param>
/// <Param name = "countryFlag"> The country flag. </param>
/// <Param name = "local"> The local. </param>
/// <Returns> country </returns>
Private string GetCountry (uint endIpOff, int countryFlag, out string local)
{
String country = string. Empty;
Uint offset = endIpOff + 4;
Switch (countryFlag)
{
Case 1:
Case 2:
Country = GetFlagStr (ref offset, ref countryFlag, ref endIpOff );
Offset = endIpOff + 8;
Local = (1 = countryFlag )? String. Empty: GetFlagStr (ref offset, ref countryFlag, ref endIpOff );
Break;
Default:
Country = GetFlagStr (ref offset, ref countryFlag, ref endIpOff );
Local = GetFlagStr (ref offset, ref countryFlag, ref endIpOff );
Break;
}
Return country;
}
Private string GetFlagStr (ref uint offset, ref int countryFlag, ref uint endIpOff)
{
Int flag = 0;
While (true)
{
Flag = data [offset];
// No redirection
If (flag! = 1 & flag! = 2)
{
Break;
}
If (flag = 2)
{
CountryFlag = 2;
EndIpOff = offset-4;
}
Offset = (uint) data [1 + offset] + (uint) data [2 + offset]) <8) + (uint) data [3 + offset]) <16 );
}
If (offset <12)
{
Return string. Empty;
}
Return GetStr (ref offset );
}
/// <Summary>
/// Read the string...
/// </Summary>
/// <Param name = "offset"> </param>
/// <Returns> </returns>
Private string GetStr (ref uint offset)
{
Byte lowByte = 0;
Byte highByte = 0;
StringBuilder stringBuilder = new StringBuilder (16 );
While (true)
{
LowByte = data [offset ++];
If (lowByte = 0)
{
Return stringBuilder. ToString ();
}
If (lowByte> 0x7f)
{
HighByte = data [offset ++];
If (highByte = 0)
{
Return stringBuilder. ToString ();
}
StringBuilder. Append (encoding. GetString (new byte [] {lowByte, highByte }));
}
Else
{
StringBuilder. Append (char) lowByte );
}
}
}
}
}
Create an IPSearch. ashx file as follows:
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> 1 using System;
Using System. Collections;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Xml. Linq;
Namespace AjaxIP
{
/// <Summary>
/// Brief description of IP address query
/// </Summary>
Public class IPSearch: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
String ip = context. Request ["ip"];
String ipFilePath = @ "\ App_Data \ QQWry. dat ";
QQWryLocator QQWry = new QQWryLocator (ipFilePath );
IPLocation loc = QQWry. Query (ip );
Context. Response. Write (string. Format ("{0} {1}", loc. Country, loc. Local ));
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}
}
Finally, on the Default. aspx page, write the following information about js and IP Users:
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> 1 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script language = "javascript" src = "Js/jquery-1.3.1.js"> </script>
<Script language = "javascript">
$ (Document). ready (function (){
$ ("# Tb tr"). each (function (){
Var obj = $ (this). children ("td: eq (2 )");
SearchIP (obj );
});
})
Function SearchIP (obj ){
$. Ajax ({
Type: "GET ",
Url: "IPSearch. ashx? Ip = "+ obj. text (),
Success: function (data ){
Obj. text (data );
}
});
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Table id = "tb" style = "width: 100%;">
<Thead>
<Th> 321321 </th>
<Th> 321321 </th>
<Th> 321321 </th>
</Thead>
<Tr>
<Td>
OMEGA </td>
<Td>
0 </td>
<Td>
122.229.191.8 </td>
</Tr>
<Tr>
<Td>
Deserted year </td>
<Td>
900,000 </td>
<Td>
110.87.98.30 </td>
</Tr>
<Tr>
<Td>
Han Fei </td>
<Td>
1,854,257,979 </td>
<Td>
220.188.193.72 </td>
</Tr>
<Tr>
<Td>
Hattaru </td>
<Td>
600,100 </td>
<Td>
220.188.193.72 </td>
</Tr>
<Tr>
<Td>
Makeup </td>
<Td>
400,100 </td>
<Td>
220.188.193.72 </td>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>
In this way, our background user information is no longer an unfriendly IP address segment.
Run the command to check the effect.