0

I am trying to get the IP address of user. I need to enter the IP address of user in database. My app is running on mobile which i am creating in Flutter. I am using MySql and PHP as backend.

What i have tried so far.

  1. Get IP flutter plugin But that is not returning correct IP. It is returning IP address assigned by Router.

I .

Future<void> initPlatformState() async {
        String ipAddress;
        try {
          ipAddress = await GetIp.ipAddress;
       } catch (err) {
          ipAddress = 'Failed to get ipAddress.';
          print(ipaddress);
        }
 if (!mounted) return;
    setState(() {
      _ip = ipAddress;
      print(_ip);
    });
  }
  1. Using Ipify.org to get the IP address. But that is a paid option. After 10K requests we need to purchase the service.

            const url1 = 'https://api.ipify.org';
            var response1 = await http.get(url1).timeout(Duration(seconds: 5),
                onTimeout: (){
                    //  throw Exception();
                    setState((){
                    visible = false ; 
                    }
                    );
                    //or you can also
                  return null;
                });
    
                if(response1.statusCode == 200) {
                  // The response body is the IP in plain text, so just
                  // return it as-is.
                  ipaddress = response1.body;
                }
    

Now i read earlier that from PHP we can get the IP address by using $_SERVER['REMOTE_ADDR']; But not sure it will work in this case or not.

Any advise how you guys are getting IP address.

Roxx
  • 3,738
  • 20
  • 92
  • 155
  • _"we can get the IP address by using $_SERVER['REMOTE_ADDR']; But not sure it will work in this case or not."_ - Why wouldn't it? Have you tried it? Is the server behind some proxy, load balancer or similar? – M. Eriksson May 02 '20 at 09:52
  • @MagnusEriksson i didn't tried it. I was in the assumption that it will return server IP. I am trying now. – Roxx May 02 '20 at 09:54
  • Does this answer your question? [What is the most accurate way to retrieve a user's correct IP address in PHP?](https://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php) – M. Eriksson May 02 '20 at 09:56
  • @MagnusEriksson let me check. – Roxx May 02 '20 at 09:57
  • Please always do some proper research and make some real attempts before posting. Don't just assume things don't work, read up on it and test. If you just google something like "get client ip in php" you will find a ton of examples. – M. Eriksson May 02 '20 at 09:58
  • @MagnusEriksson Server['Remote_Addr'] is returning router IP address not the actual address. my case it is returning 192.168.100.2. – Roxx May 02 '20 at 10:01
  • I have no idea what your setup is. The server gets a request from the client. If the client is behind a router, the client will have an internal IP (usually starting with 192.168.x.x) but when it makes a request to somewhere outside of that local network, it's the external IP (the router) the rest of the world sees. That's correct and how it should be. Now I have no idea what routers/proxys/vpns/load balancers you have (if any) or what IP you expect to get or which you're getting. Please edit the question to include all that info or we won't have a chance of understanding what's going on – M. Eriksson May 02 '20 at 10:10

1 Answers1

-1

You can use https://ip.seeip.org/jsonip? to get the public IP in JSON format.

It's FREE.

If you want to make your own in PHP use the code below.

<?php
exit(json_encode(["ip" => $_SERVER['REMOTE_ADDR']]));
Xihuny
  • 1,410
  • 4
  • 19
  • 48
  • What if the web server is behind a proxy/load balancer? Then `$_SERVER['REMOTE_ADDR']` will show the proxy's/load balancers IP, not the clients. Since there already are _many_ questions/answers here on SO about getting the IP in PHP, there's no need to write another one. – M. Eriksson May 02 '20 at 10:00