Obtain the real ip address of the client and the ip address of the client

Source: Internet
Author: User
Tags servervariables

Obtain the real ip address of the client and the ip address of the client

1. Need to introduce log4j-1.2.14.jar

Package org. ydd. test; import java. util. enumeration; import javax. servlet. http. httpServletRequest; import org. apache. log4j. logger;/*** @ author coco * @ version * Get IP4 */public class IP4 {private static final Logger log = Logger. getLogger (IP4.class);/*** get the real IP address of the client (Note: if you use the Squid reverse proxy software, you need to change the squid. the default value of the conf configuration file forwarded_for is on. * otherwise, the obtained IP address is unknown. * @ param request * @ return */@ SuppressWarnings ({"rawtypes", "Unchecked"}) public static String getIpAddr (HttpServletRequest request) {log. debug ("============================================ ================== "); if (log. isDebugEnabled () {StringBuffer buf = new StringBuffer ("all head info: \ n"); Enumeration enumeration = request. getHeaderNames (); while (enumeration. hasMoreElements () {Object head = enumeration. nextElement (); if (null! = Head) {String value = request. getHeader (String. valueOf (head); buf. append (head + "=" + value + "\ n") ;}} log. debug (buf. toString ();} String ip = request. getHeader ("x-forwarded-for"); log. debug ("request. getHeader (\ "x-forwarded-for \") = "+ ip); if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getHeader ("X-Forwarded-For"); log. debug ("request. getHeader (\ "X-Forwarded-For \") =" + Ip);} if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getHeader ("Proxy-Client-IP"); log. debug ("request. getHeader (\ "Proxy-Client-IP \") = "+ ip);} if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getHeader ("WL-Proxy-Client-IP"); log. debug ("request. getHeader (\ "WL-Proxy-Client-IP \") = "+ ip);} if (ip = null | ip. length () = 0 | "unk Nown ". equalsIgnoreCase (ip) {ip = request. getHeader ("HTTP_CLIENT_IP"); log. debug ("request. getHeader (\ "HTTP_CLIENT_IP \") = "+ ip);} if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getHeader ("HTTP_X_FORWARDED_FOR"); log. debug ("request. getHeader (\ "HTTP_X_FORWARDED_FOR \") = "+ ip);} if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getRem OteAddr (); log. debug ("request. getRemoteAddr () =" + ip);} if (null! = Ip & ip. indexOf (',')! =-1) {// If a multi-level reverse proxy is passed, there are more than one X-Forwarded-For value, it is a string of IP values // take the first valid IP string not unknown in X-Forwarded-For // For example: X-Forwarded-For: 192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100 // your real IP address is 192.168.1.110 // Note: When the access address is localhost, the address format is 0: 0: 0: 0: 0: 0: 0: 1log. debug ("ip =" + ip); String [] ips = ip. split (","); for (int I = 0; I <ips. length; I ++) {if (null! = Ips [I] &! "Unknown ". equalsIgnoreCase (ips [I]) {ip = ips [I]; break;} if ("0: 0: 0: 0: 0: 0: 0: 1 ". equals (ip) {log. warn ("because the client access address uses localhost and the real IP address of the client is incorrect, Please access it using IP Address") ;}} if ("unknown ". equalsIgnoreCase (ip) {log. warn ("An error occurred while obtaining the real IP address of the client through the Squid reverse proxy software. Please change the squid. conf configuration file forwarded_for is resolved on by default ");} log. debug ("============================================ ================== "); return ip ;}}


ASP obtains the real IP address of the client ??

Use Request. serverVariables ("REMOTE_ADDR") to obtain the IP address of the client, but if the client is accessed by the proxy server, the obtained IP address is the IP address of the proxy server, not the real IP address of the client. To obtain the real IP address of the client through the proxy server, use Request. ServerVariables ("HTTP_X_FORWARDED_FOR") to read. However, not every proxy server can use Request. serverVariables ("HTTP_X_FORWARDED_FOR") to read the real IP address of the client, some of which are still the IP address of the proxy server. Note that if the client is not accessed through the proxy server, the value obtained using Request. ServerVariables ("HTTP_X_FORWARDED_FOR") will be null. Therefore, if you want to use this method in the program, you can do this:
......
Userip = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If userip = "" Then userip = Request. ServerVariables ("REMOTE_ADDR ")
......

That is, if the client uses the proxy server, the value of HTTP_X_FORWARDED_FOR is obtained. If the client does not pass the proxy server, the value of REMOTE_ADDR is obtained.

'Common function: if the real IP address of the client cannot be obtained, the proxy IP address of the client is obtained.
Private Function getIP ()
Dim strIPAddr
If Request. ServerVariables ("HTTP_X_FORWARDED_FOR") = "" OR InStr (Request. ServerVariables ("HTTP_X_FORWARDED_FOR"), "unknown")> 0 Then
StrIPAddr = Request. ServerVariables ("REMOTE_ADDR ")
ElseIf InStr (Request. ServerVariables ("HTTP_X_FORWARDED_FOR"), ",")> 0 Then
StrIPAddr = Mid (Request. ServerVariables ("HTTP_X_FORWARDED_FOR"), 1, InStr (Request. ServerVariables ("HTTP_X_FORWARDED_FOR"), ",")-1)
ElseIf InStr (Request. ServerVariables ("HTTP_X_FORWARDED_FOR"), ";")> 0 Then
StrIPAddr = Mid (Request. ServerVariables ("HTTP_X_FORWARDED_FOR"), 1, InStr (Request. ServerVariables ("HTTP_X_FORW... remaining full text>

Obtain the Client IP Address

If you use localhost for access during the test, the server obtains 127.0.0.1, because localhost is a loop address, which is special.
If you access through your IP address, the obtained IP address will be correct.

-------
To access the service through your local IP address, find the local connection in the network connection of the control panel, view its properties, and find the "details" button, in the dialog box that appears, the IPV4 address is the local address.
Or open the command line and enter ipconfig/all to find the local address.
Access through this IP address in the browser will not be 127.0.0.1.

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.