How do I get the remote address of a client in servlet?

try this: public static String getClientIpAddr(HttpServletRequest request) { String ip = request.getHeader(“X-Forwarded-For”); if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = request.getHeader(“Proxy-Client-IP”); } if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = request.getHeader(“WL-Proxy-Client-IP”); } if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = … Read more

How to get a Docker container’s IP address from the host

The –format option of inspect comes to the rescue. Modern Docker client syntax is: docker inspect -f ‘{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ container_name_or_id Old Docker client syntax is: docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ container_name_or_id These commands will return the Docker container’s IP address. As mentioned in the comments: if you are on Windows, use double quotes ” instead … Read more

What is the most accurate way to retrieve a user’s correct IP address in PHP?

Here is a shorter, cleaner way to get the IP address: function get_ip_address(){ foreach (array(‘HTTP_CLIENT_IP’, ‘HTTP_X_FORWARDED_FOR’, ‘HTTP_X_FORWARDED’, ‘HTTP_X_CLUSTER_CLIENT_IP’, ‘HTTP_FORWARDED_FOR’, ‘HTTP_FORWARDED’, ‘REMOTE_ADDR’) as $key){ if (array_key_exists($key, $_SERVER) === true){ foreach (explode(‘,’, $_SERVER[$key]) as $ip){ $ip = trim($ip); // just to be safe if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){ return $ip; } } } } … Read more