This article introduces the example code to obtain the IP address accessed by the client browser, share it with everyone, and leave a note for yourself.
1. js method
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <script src="/cityjson?ie=utf-8"></script> <script type="text/javascript"> ('IP address:' + returnCitySN["cip"] + ', City code:' + returnCitySN["cid"] + ', area:' + returnCitySN["cname"]); </script> </head> <body> </body> </html>
2. Background code implementation
#region IP address restriction function 2017-07-18 /// <summary> /// /// </summary> /// <returns></returns> public bool IsIPValidate() { bool flag = false; string userip = GetLoginIp(); string[] addr = GetAddressByIp(userip); string addrs = addr[0] + addr[1]; if ("Beijing".Equals(addr[0]) || "Beijing".Equals(addr[1])) { flag = true; } return flag; } /// <summary> /// Get the IP address of the remote access user /// </summary> /// <returns>Return IP address</returns> protected string GetLoginIp() { string loginip = ""; //[""]--Get the service variable collection if (["REMOTE_ADDR"] != null) //Discern whether the IP address of the remote host that issued the request is empty { //Get the IP address of the remote host that issued the request loginip = ["REMOTE_ADDR"].ToString(); } //Discern whether the registered user uses the Setup Agent else if (["HTTP_VIA"] != null) { if (["HTTP_X_FORWARDED_FOR"] != null) { //Get the proxy's server IP address loginip = ["HTTP_X_FORWARDED_FOR"].ToString(); } else { //Get client IP loginip = ; } } else { //Get client IP loginip = ; } return loginip; } /// <summary> /// Obtain the province and city according to IP /// </summary> public string[] GetAddressByIp(string ip) { string PostUrl = "/iplookup/?ip=" + ip; string res = GetDataByPost(PostUrl);//The data returned by this request is: res=1t115.193.210.0t115.194.201.255tChina tZhejiang t Hangzhou t Telecom string[] arr = getAreaInfoList(res); return arr; } /// <summary> /// Post request data /// </summary> /// <param name="url"></param> /// <returns></returns> public string GetDataByPost(string url) { HttpWebRequest req = (HttpWebRequest)(url); string s = "anything"; byte[] requestBytes = (s); = "POST"; = "application/x-www-form-urlencoded"; = ; Stream requestStream = (); (requestBytes, 0, ); (); HttpWebResponse res = (HttpWebResponse)(); StreamReader sr = new StreamReader((), ); string backstr = (); (); (); return backstr; } /// <summary> /// Process the required data /// </summary> /// <param name="ip"></param> /// <returns></returns> public static string[] getAreaInfoList(string ipData) { //1t115.193.210.0t115.194.201.255t China t Zhejiang t Hangzhou t Telecom string[] areaArr = new string[10]; string[] newAreaArr = new string[2]; try { //Get the desired data, only the province and city are taken here areaArr = ('t'); newAreaArr[0] = areaArr[4];//Province newAreaArr[1] = areaArr[5];//city } catch (Exception e) { } return newAreaArr; } #endregion
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.