Capture Real User IP

Asked

Viewed 5,763 times

5

How do I capture the actual Public IP/IP of the user accessing the application? The IP address of the computer I can capture normally, but I want to know the public IP address of this client.

4 answers

5

  • This code returns the machine’s local IP and not its internet IP. So much so that if you access http://www.meuip.com.br and create an application with the informed code, you will see that the two different ip’s.

  • @Zackmorgan But the goal is to get the IP of who is accessing my application. Not my public IP.

  • This application IP is of no use to me in a judicial audit process. Because you see, my machine can have IP 10.10.1.20 in my network. But also, in another network of a neighbor, he may also have the same IP address. Then, with the Real IP, if necessary, I contact the provider, he tells me the necessary information and consequently, if you take the necessary measures. In my application I already capture the IP of the computer, which is totally "useless" for me.

  • @Zackmorgan is not the IP of the computer. It is the IP remote of client, in this case, who is accessing your application. The documentation makes this clear.

  • I did the following, I do it on the client’s side. Via javascript, because whoever requests javascript to some service that provides the real IP is the client. Because I need this type of IP. That way, I have a variable that sends this IP to my controller, and I also capture this remote IP. If it’s the best way I haven’t, but it worked, it did.

  • I find it a little too laborious this way, but I admit it works.

Show 1 more comment

5

I get it like this:

        public static string GetPublicIP()
        {
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string a4 = a3[0];
            return a4;
        }

2

add the using

using System.Net;                

include the code below

string host = Dns.GetHostName();
string ip = Dns.GetHostAddresses(host)[2].ToString();

0

try to use this

<?php

    $clientIP = $_SERVER['HTTP_CLIENT_IP'] 
        ?? $_SERVER["HTTP_CF_CONNECTING_IP"] # when behind cloudflare
        ?? $_SERVER['HTTP_X_FORWARDED'] 
        ?? $_SERVER['HTTP_X_FORWARDED_FOR'] 
        ?? $_SERVER['HTTP_FORWARDED'] 
        ?? $_SERVER['HTTP_FORWARDED_FOR'] 
        ?? $_SERVER['REMOTE_ADDR'] 
        ?? '0.0.0.0';
    
    # Earlier than PHP7
    $clientIP = '0.0.0.0';
    
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $clientIP = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        # when behind cloudflare
        $clientIP = $_SERVER['HTTP_CF_CONNECTING_IP']; 
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $clientIP = $_SERVER['HTTP_X_FORWARDED'];
    } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $clientIP = $_SERVER['HTTP_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
        $clientIP = $_SERVER['HTTP_FORWARDED'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $clientIP = $_SERVER['REMOTE_ADDR'];
    }
    
    echo "Meu cliente IP: ". $clientIP;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.