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.
- 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);
});
}
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.